Моя проблема заключается в том, что при отправке формы и возврате моего видаModel типа QuestionViewModel - Тип имеет значение null.ASP.net, MVC 2 - Posting viewModel
См. Код ниже.
Мой контроллер:
public ActionResult Create()
{
var viewModel = new QuestionViewModel {
Question = question,
Type = questionType,
Types = surveyDB.QuestionTypes.ToList()
};
return View(viewModel);
}
На мой взгляд:
`
<h2>Skapa en fråga</h2>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<%:Html.EditorFor(model => model.Question, new { Types = Model.Types }) %>
<% switch (Model.Question.Type) {
case 1:
Response.Write(Html.EditorFor(model => model.Type));
break;
default:
break;
}
%>
<p>
<input type="submit" value="Skapa" />
</p>
<% } %>
`
Где редактор для model.Question является
<%: Html.LabelFor(model => model.Type) %>
<%:Html.DropDownList("Type", new SelectList(ViewData["Types"] as IEnumerable,"Id","Type", Model.Type), "Välj en frågetyp")%>
<% if((int)ViewData["type"] > 0) { %>
<div class="editor-label">
<%: Html.LabelFor(model => model.Text) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Text) %>
<%: Html.ValidationMessageFor(model => model.Text) %>
</div>
И редактор model.Type
<%: Html.LabelFor(model => model.Alternative) %>
<%: Html.TextAreaFor(model => model.Alternative, new { @Class="alternatives" })%>
<%: Html.ValidationMessageFor(model => model.Alternative)%>
Когда я присылают я заканчиваю здесь:
[HttpPost]
public ActionResult Create(QuestionViewModel viewModel)
{
**// This becomes null**
string alternatives = viewModel.Type.Alternatives;
}
Мой ViewModel выглядит следующим образом
namespace ASurvey.ViewModels {
public class QuestionViewModel {
public Question Question { get; set; }
public List<QuestionType> Types { get; set; }
public MultipleChoice Type { get; set; }
}
}
Имеет ли MultipleChoice конструктор по умолчанию, чтобы MVC мог его создать? Вы не отправили код _all_. – queen3