开发者

Getting posted values in MVC PartialView

I've created a PartialView which I render with Html.RenderPartial, passing the name of the view and the strongly-typed data item to bind to (below):

    <% Html.RenderPartial("SearchViewUserControl", ViewData["SearchData"]); %>

The partial view has a form containing a submit button:

<% using (Html.BeginForm("Search", "Home"))
   { %>
             ...
    <div>
        <input type="submit" value="Search" />
    </div>
<% } %>

I've set a breakpoint in my controller's action method (below) but nothing is set in searchData. What am I doing wrong?

   public ActionResult Search(SearchDomain searchData)开发者_StackOverflow社区
    {
        if (ModelState.IsValid)
        {
        }

        return View();
    }


You need to post the actual form elements for anybody to know whats wrong.

The form html is what sets the binding to SearchDomain. You want to have your form elements named like this:

<input name="searchData.SomeProperty">

For them to bind to your action parameter.


In order to pull a SearchDomain object out of your view from a controller method, your view has to either inherit from System.Web.Mvc.ViewPage<Models.SearchDomain>, or a custom ViewModel class that contains a SearchDomain object.

The other way to do it is to have your view inherit from System.Web.Mvc.ViewPage, and use UpdateModel to cast the view data to a SearchDomain object. Something like this:

public ActionResult Save()
{
        SearchDomain domain = new SearchDomain ();

        UpdateModel(domain , new[] { "Name", "Email", "Phone", ... });
        return View(domain); 
}


To be honest, I think RenderAction is much easier to use.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜