【问题标题】:When I render a partial view my Model is Null当我渲染部分视图时,我的模型为 Null
【发布时间】:2018-01-10 11:34:37
【问题描述】:

我正在尝试在 ASP.NET MVC 的另一个视图中呈现一个仅由 DropDownList 组成的视图,但由于某种原因,模型为空。我认为这是因为我只调用视图而根本没有调用控制器。我哪里错了?也许我的电话方法不正确。

Dropbox 视图

@model MyWatch.Models.Countries
@{ 
    ViewBag.Title = "CountrySearch";
    Layout = null;
    if (Model != null)
    {
        Html.DropDownListFor(m => m.countries, new SelectList(Model.countries.Select(s => new SelectListItem { Text = s.name, Value = s.code })));
    }
    else
    {
        Html.DropDownListFor(m => m.countries, new SelectList(new List<String>()));
    }
}

主视图

<div class="form-group">
    @Html.LabelFor(m => m.Country, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @{Html.RenderPartial("Search/CountrySearch");}
    </div>
</div>

控制器

    public ActionResult CountrySearch()
    {
        try
        {
            using (StreamReader streamReader = new StreamReader("C:\\Users\\Alex Combe\\Documents\\Visual Studio 2015\\Projects\\MyWatch\\MyWatch\\App_Data\\CountryList.json"))
            {
                string json = streamReader.ReadToEnd();
                Countries countries = new Countries();
                countries.countries = JsonConvert.DeserializeObject<IList<Country>>(json);
                return View(countries);
            }
        }
        catch (FileNotFoundException e)
        {
            Console.WriteLine(e.StackTrace);
            return View(new Countries());
        }
    }
}

型号

namespace MyWatch.Models
{
    public class Country
    {
        public string name { get; set; }
        public string code { get; set; }
        public SelectList selectList { get; set; }
    }

    public class Countries
    {
        public IList<Country> countries;
    }
}

【问题讨论】:

  • 因为您没有将模型传递给局部视图,例如Html.RenderPartial("Search/CountrySearch", Model);
  • 使用 Action() 方法代替 RenderPartial。 RenderPartial 不会通过控制器,它直接进入视图。

标签: c# asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

您正在尝试直接渲染视图,您应该首先调用控制器操作。请改用Html.RenderAction

<div class="col-md-10">
    @{Html.RenderAction("CountrySearch","Search");}
</div>

并返回PartialView 而不是View

return PartialView(countries);

另外,DropDownListFor 第一个参数应该被选中。

Html.DropDownListFor(m => m.SelectedItem, new SelectList(Model.countries.Select(s => new SelectListItem { Text = s.name, Value = s.code })));

也改变你的模型;

public class Countries
{
    public string SelectedItem { get; set; }
    public IList<Country> countries;
}

并确保CountryList.json 具有适用于您的模型的有效数据。

【讨论】:

  • 嘿,非常感谢你们两位的反馈。我已经进行了您建议的更改,但现在当我尝试呈现注册页面时出现以下 InnerException:InnerException {"The controller for path '/Account/Register' was not found or does not implement IController."} System.Exception {System.Web.HttpException}
  • @AlexanderCombe 该错误似乎与此处的代码无关,您应该使用相关代码提出一个单独的问题。
  • 正如@ADyson 所描述的,这与这个问题无关。
【解决方案2】:

@Html.Partial@Html.RenderPartial 方法并不意味着调用 Controller 的 Action 方法,而是这些方法将直接从给定模型填充 Partial View 的标记并渲染它。如果没有传递模型,那么它只会渲染视图标记,就好像模型是空的一样。

如果你想使用 RenderPartial,你需要在方法中直接传递一个模型对象,作为第二个参数 - 参见https://msdn.microsoft.com/en-us/library/system.web.mvc.html.renderpartialextensions.renderpartial(v=vs.118).aspx#M:System.Web.Mvc.Html.RenderPartialExtensions.RenderPartial%28System.Web.Mvc.HtmlHelper,System.String,System.Object%29的文档

例如@Html.RenderPartial("Search/CountrySearch", yourModelObject)

如果你想通过你的控制器动作来实现它,你必须调用@Html.RenderAction("CountrySearch", "Search"),并让CountrySearch方法以return PartialView结束而不是return View

【讨论】:

  • 感谢您的反馈
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
  • 2021-03-22
相关资源
最近更新 更多