English 中文(简体)
Asp.net mvc 2 form not posting to controller through Ajax
原标题:Asp.net mvc 2 form not posting to controller via Ajax

i m 试图通过麻省向我的控制员提供一种表格,这样就可以得出部分看法。

我的阿贾克斯法典

 var formCollection = $( #some-form );
 $(function(){ $( #some-form ).submit(function(){
      $.ajax({
           type: "POST",
           url: "/Trusk/Index",
           data: formCollection,
           dataType: "html",
           success: function (result) {
               $( #newView ).html(result);
           },
           error: function (request, status, error) {
               alert( Oh no! );
           }
     });
});
}); 

我形式的法典,即希望在ID = 新的版本中作部分考虑。 观点 部分意见由控制人退回

  <% using (Html.BeginForm(new { @id = "some-form" }))
           { %>
            <div id="TestDiv">
            <div id="Title">Test</div>                 
                <div id="CheckIn">Check-in:<br />
                <%:Html.TextBox("FromDate", "", new { @id = "DateFrom", @style =  "width:190px" })%></div>
                <div id="CheckOut">Check-out:<br />
                <%:Html.TextBox("ToDate", "", new { @id = "DateTo", @style =  "width:190px" })%><br /></div>
                <div id="newView">  
                </div>
                <input type="submit" value="Search" />        
        </div>
               </div>
        <% } %>

我的主计长

 public ActionResult Index(FormCollection post)
    {
        ITruckRepository hotelRep = new TruckRepository();

        IEnumerable<Truck> AvailableTrucks = hotelRep.getTrucks( 2012,3,2 ,  2012,3,5 );

       return PartialView("Search", AvailableTrucks );

    }

是否通过Ajax将正确的参数转给控制人员?

成就

最佳回答

你们的法典存在几个问题:

  • You are using a wrong overload of the Html.BeginForm method. In the overload you used the id is a route value, not HTML attribute
  • Inside your .submit callback you are not canceling the default action by returning false and thus when the form is submitted the AJAX call will hardly have any time to execute before the browser redirects away from the page
  • In the data parameter of the AJAX call you are passing a jQuery object representing the form called formCollection when you should be serializing the data.
  • You have broken markup => there s one closing div that doesn t have a corresponding opening element.

因此,首先要确定标识:

<% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "some-form" })) { %>
    <div id="TestDiv">
        <div id="Title">Test</div>                 
        <div id="CheckIn">
            Check-in:<br />
            <%= Html.TextBox("FromDate", "", new { id = "DateFrom", style = "width:190px" }) %>
        </div>
        <div id="CheckOut">
            Check-out:<br />
            <%= Html.TextBox("ToDate", "", new { id = "DateTo", style = "width:190px" }) %>
            <br />
        </div>
        <div id="newView"></div>
        <input type="submit" value="Search" />        
    </div>
<% } %>

以及随后将修改以下表格的文字:

$(function () {
    $( #some-form ).submit(function () {
        $.ajax({
            type: this.method,
            url: this.action,
            data: $(this).serialize(),
            success: function (result) {
                $( #newView ).html(result);
            },
            error: function (request, status, error) {
                alert( Oh no! );
            }
        });
        return false;
    });
}); 

现在,你们应当确保成功援引相应的《管制局控制员》行动,并正确通过参数。 还确保在控制者的行动中不发生错误,同时提出部分意见。 采用“火焰”等javascript debugging工具,准确了解发出何种要求/答复以及任何可能的碎片错误。

问题回答

您需要打电话Serialize( on formCollection - which will encode the form as a string for submission.

var formCollection = $( #some-form ); 
$(function(){ $( #some-form ).submit(function(){ 
  $.ajax({ 
       type: "POST", 
       url: "/Trusk/Index", 
       data: formCollection.serialize(), 
       dataType: "html", 
       success: function (result) { 
           $( #newView ).html(result); 
       }, 
       error: function (request, status, error) { 
           alert( Oh no! ); 
       } 
 }); 
});
});  

另外,尽管您在控制器方法上有一个参数,但您并未实际使用该参数?





相关问题
Getting rid of Microsoft AJAX

We wrote a couple of controls using Microsoft AJAX (cs class + js class). Now I m thinking about getting rid of it (just use jQuery), as it is bloated and we don t use UpdatePanel. My question is: how ...

ASP.NET Ajax 4 and Microsoft Ajax library definitions

I undestand that is former for Web Forms ASP.Net only , latter ( currently preview 6 ) suitable for both - classic and MVC. Would it be possible to give a brief description/usage for each. By ASP.NET ...

ASP.NET Rich UI - What do you use? [closed]

I m looking for feedback on what other people use on their ASP.NET projects to provide a rich user interface experience while still remaining as productive as possible. I m developing an ASP.NET 3.5 ...

CollapsiblePanelExtender collapses on link hover

I am trying to use an CollapsiblePanelExtender to make some dropdown menus and am having some problems when hovering over links - can anyone help me out... By the way I do want to keep the ...

热门标签