Answer the question
In order to leave comments, you need to log in
How to create and display elements in the same view (the view is strongly typed)?
There were two strongly typed views: Summary (for displaying cities) and Create (for creating new cities)
in Summary: @model IEnumerable<City>
, in Create @model City
Both display and create worked fine.
I merged the views, and Create stopped working (In the controller, the Post method comes Null ).
I suspect the problem is that the @model directive is now IEnumerable.
Tell me what could be the problem.
Answer the question
In order to leave comments, you need to log in
We need to create a new model that will include IEnumerable<City> and City .
More or less like this:
public class NewModel
{
public IEnumerable<City> CitiesList { get; set; }
public City City { get; set; }
}
@foreach(var city in Model.CitiesList)
{
// список
}
@Model.City.КакоетоСвойство
@model NewMode
@Html.Partial("CitiesList", Model.CitiesList)
@Html.Partial("CityEditor", Model.City)
@model IEnumerable<City>
// ...
@model City
// ...
@model is required for rendering , it should not affect model binding .
That is, if the method matches your routing Create(City city)
, then the binding will try to find among the ValueProviders the values that match the names of the model properties (in this case - City
).
What binding errors are you getting?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question