[英]JS grabbing text from text-boxes, passing to asp.net mvc ActionResult but ActionResult parameters appear null
JS grabbing text from text-boxes, passing to asp.net mvc ActionResult but ActionResult parameters appear null JS从文本框中获取文本,传递给asp.net mvc ActionResult,但ActionResult参数显示为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. 我有2个文本框,由用户选择时由2个日期选择器填充,但是当我使用下面的javascript将其传递给actionresult时,虽然信息可以正确地从文本框中获取。 The parameters of the actionresult show to be null. actionresult的参数显示为空。
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. 这就是我的意思-但首先,我个人还是觉得Ajax与MVC控制器的绑定有时很痛苦。 It's flexible, but if anything at all is out of place, you just get nulls - as you know. 它很灵活,但是,如果有什么不合时宜的地方,您就会得到null-如您所知。 Anyway... 无论如何...
[HttpPost]
public ActionResult FirstDoc(MyObject inputObj)
{
//use inputObj here as needed
}
Then, you need a definition for the MyObject - to keep it simple 然后,您需要为MyObject定义-使其简单
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. 如果所有鸭子都排成一排,当然包括ajax一侧的选项,则对象“应该”绑定。
The way you are passing the data
would work for type: 'get'
but not for post
. 您传递data
方式适用于type: 'get'
但不适用于post
。 Try this: 尝试这个:
data: JSON.stringify({ start: startD, end: endD }),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.