简体   繁体   中英

Passing parameters to a partial view using Html.Action

I am developing an MVC application (razor syntax), and I am having issues passing parameters into my Html.Action call. Below is a search page I am implementing. Note, the model contains distinct values to be used in the dropdowns.

Call From View:

@using(Html.BeginForm("Search", "Home", FormMethod.Get, null)){

@Html.LabelFor(model => model.siteCode)
@Html.DropDownListFor(model => model.siteCode, new SelectList(Model.siteCode), "All")

@Html.LabelFor(model => model.group)
@Html.DropDownListFor(model => model.group, new SelectList(Model.group), "All")
}

@Html.Action("ShowOrderData", "Home", new {siteCode = Model.siteCode, group = Model.group}

Action in Controller:

public ActionResult ShowOrderData(string siteCode = "",
                                        string group = "",)
{
        var model = _db.ToolOrders
            .Where(r => r.SiteCD_LOC.Contains(siteCode))
            .Where(r => r.GroupCode.Contains(group))

return PartialView("_OrderData", model);
}

Note: There may be some syntactical errors. There are about 10 more parameters being passed and I didn't want to clog up the code blocks.

So when debugging with this code, empty strings are sent to my ShowOrderData controller. I attempted to add .ToString in the Html.Action call ( Model.siteCode.ToString() ), and a LINQ statement will show up instead. I am guessing this method is wrong because the model does not contain any information from the previous request.

How do I properly send data from the QueryString to a partial view? I've seen methods that access the queryString directly via request.querystring, but I've also heard that it is bad practice to use that in a view.

Thanks

Use:

  @Html.RouteLink()

instead of action

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM