简体   繁体   English

JS从文本框中获取文本,传递给asp.net mvc ActionResult,但ActionResult参数显示为null

[英]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.

相关问题 在两个文本框上进行验证或输入验证 - ASP.NET Webforms - Either or type validation on 2 text-boxes - ASP.NET Webforms Window.location.href 将参数发布到 actionresult asp.net mvc - Window.location.href post parameters to actionresult asp.net mvc 没有提交的ASP.NET MVC ActionResult POST - ASP.NET MVC ActionResult POST without Submit 将ActionResult返回到对话框。 ASP.NET MVC - Return ActionResult to a Dialog. ASP.NET MVC ASP.NET MVC-如何显示返回到javascript的“ ActionResult”视图? - ASP.NET MVC - How to display an “ActionResult” view that is returned to javascript? 从ASP.NET MVC 5中的Controllers ActionResult更改html elemts样式 - Change html elemts style from Controllers ActionResult in ASP.NET MVC 5 参数未传递到ActionResult中 - Parameters not passing into an ActionResult angularJS将表单数据转换为ActionResult-ASP .NET MVC - angularJS Form data to ActionResult - ASP .NET MVC 我可以使用ASP.NET MVC4作为ActionResult发送消息而不是视图吗 - Can I send a message instead of a view back as an ActionResult with ASP.NET MVC4 如何在使用FileContentResult ActionResult时在客户端的ASP.NET MVC中了解文件下载状态? - How to know file download status in ASP.NET MVC in clientside while using FileContentResult ActionResult?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM