问题描述
JS抓住从文本框的文字,传递给ASP.NET MVC的ActionResult但ActionResult的参数出现空
我有2个文本框这是由2 datepickers根据用户的选择充满不过虽然从信息的文本框正确地抓住,当我使用下面的javascript来将它传递给的ActionResult。的的ActionResult显示的参数为空。
如何解决?
code:
@ Html.Label(开始,开始日期)
@ Html.TextBox(开始,的String.Empty,新{@id =开始,@class =日期选择器})
@ Html.Label(endd,结束日期)
@ Html.TextBox(endd的String.Empty,新{@id =结束,@class =日期选择器})
<输入类型=提交值=应用ID =DateSelected/>
<脚本类型=文/ JavaScript的>
$('。日期选择器)日期选择器()。
$(#DateSelected)。点击(函数(){
VAR startD = $('#开始)VAL()的toString()。
。VAR endD = $('#终结)VAL()的toString();
$阿贾克斯({
网址:'/文档/ FirstDoc',
键入:邮报,
数据:{开始:将String.valueOf(startD),结束:将String.valueOf(endD)},
});
< / SCRIPT>
操作结果:
[HttpPost]
公众的ActionResult FirstDoc(字符串的开始,结束串)
{
FirstDocModel firstDocModel = FirstDocHelper.RunFunction(开始,结束);
返回PartialView(firstDocModel);
}
这就是我说的话 - 但首先我得亲自找到了Ajax MVC控制器绑定有时是痛苦的。它是灵活,但如果在所有发现任何异常的地方,你只是得到空 - 如你所知。反正...
[HttpPost]
公众的ActionResult FirstDoc(为MyObject inputObj)
{
//使用inputObj这里需要
}
然后,你需要一个定义为MyObject的 - 要保持它的简单
公共类为MyObject {
字符串的开始{获取;设置;}
字符串结束{获取;设置;}
}
如果你所有的鸭子都是在一排,其中包括你对课程的AJAX方的选择,你的对象应当的绑定。
JS grabbing text from text-boxes, passing to asp.net mvc ActionResult but ActionResult parameters appear null
I have 2 textboxes which are filled by 2 datepickers upon the user's selection but although the information grabs correctly from the textboxes when I use the below javascript to pass it to the actionresult. The parameters of the actionresult show to be null.
How do I fix?
Code:
@Html.Label("Start", "Start Date:")
@Html.TextBox("Start", string.Empty, new {@id = "Start", @class = "datepicker"})
@Html.Label("endd", "End Date:")
@Html.TextBox("endd", string.Empty, new {@id = "End", @class = "datepicker"})
<input type="submit" value="Apply" id ="DateSelected" />
<script type="text/javascript">
$('.datepicker').datepicker();
$("#DateSelected").click(function () {
var startD = $('#Start').val().toString();
var endD = $('#End').val().toString();
$.ajax({
url: '/Docs/FirstDoc',
type: "Post",
data: { start: String.valueOf(startD), end: String.valueOf(endD) },
});
</script>
Action Result:
[HttpPost]
public ActionResult FirstDoc(string start, string end)
{
FirstDocModel firstDocModel = FirstDocHelper.RunFunction(start, end);
return PartialView(firstDocModel);
}
Here's what I was saying - but first of all I too personally find the ajax to MVC controller binding painful sometimes. It's flexible, but if anything at all is out of place, you just get nulls - as you know. Anyway...
[HttpPost]
public ActionResult FirstDoc(MyObject inputObj)
{
//use inputObj here as needed
}
Then, you need a definition for the MyObject - to keep it simple
public class MyObject{
string start {get;set;}
string end {get;set;}
}
If all your ducks are in a row, including your options on the ajax side of course, your object 'should' bind.
这篇关于JS抓住从文本框的文字,传递给ASP.NET MVC的ActionResult但ActionResult的参数会出现空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!