简体   繁体   中英

Html.Action(“MyView”, Model.SomeData) passed in as null?

Below is the short version of my code, it's very basic. In my page model, I have an enumerable field that I'm passing into an Action() method. The problem is, right before Html.Action() is invoked in the view, it's NOT null. However, once it's inside the child action, it's suddenly null - as if it's not being passed in.

On a similar attempt, I tried doing Html.Partial("OverlayAlerts", Model.Alerts) and it still passes the data in as null. I'm not sure why this is. When debugging, the Alerts property is being populated before calling the Action() (or Partial()) method. Any ideas? Thanks.

PAGE CONTROLLER ACTION

public ActionResult Index() 
{
    var model = new DashboardModel()
    {
        ...
        Alerts = GadgetService.GetAlerts()
        ...
    };

    return View(model); 
}

MODEL:

public class DashboardModel
{
    ...
    public IEnumerable<AlertMessage> Alerts { get; set; }
    ...
}

VIEW

<%: Html.Action("GetOverlayAlerts", Model.Alerts)%>

CHILD ACTION (same controller)

[ChildActionOnly]
public ActionResult GetOverlayAlerts(IEnumerable<AlertMessage> alerts)
{
    alerts.Any(); // <--- FAILS: alerts is passed through as null?
}

尝试这个:

<%: Html.Action("GetOverlayAlerts", new {alerts = Model.Alerts})%>

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