开发者

Why Ajax.BeginForm does not pass form values?

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!


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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜