Why Ajax.BeginForm does not pass form values?

China☆狼群 提交于 2019-12-13 14:37:33

问题


I'm trying to show a partial view via calling Ajax.BeginForm, but I can't receive the values of my form(I need to get the value of hidden input, bookId, in controller, e.g 5).

// View

@using (Ajax.BeginForm("Detail", "Books", new AjaxOptions { HttpMethod = "GET",         UpdateTargetId = "ShowBookDiv" }))
{ 
    <input type="hidden" id="bookId" value="5" />
    <input type="submit" id="sBtn" value="Details"  />
}

// Controller

[HttpGet]
public ActionResult Detail(string bookId)
{                               
    if (Request.IsAjaxRequest())    
    {
        var a = Request["bookId"].ToString();
        // some code to get details
        return PartialView("ShowBooks", details);
    }
    ...
}

When I trace the code in Controller bookId is null!


回答1:


I've added the "name" property to hidden field and it works !!! really strange!

 <input type="hidden" name="bookId" id="bookId" value="5" />



回答2:


Ajax.BeginForm is a pain, IMO.

I would Use $.ajax from JQuery Ajax API :

http://api.jquery.com/jQuery.ajax

here is a good example for you to see how it works :

http://www.tugberkugurlu.com/archive/working-with-jquery-ajax-api-on-asp-net-mvc-3-0-power-of-json-jquery-and-asp-net-mvc-partial-views

Posting the whole form requires a little bit of work (in terms of validation, etc.) but you will have complete control over the action if you are good with JavaScript.



来源:https://stackoverflow.com/questions/7579263/why-ajax-beginform-does-not-pass-form-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!