1
我的问题是,当我提交表单并返回类型QuestionViewModel的viewModel - 类型为null。ASP.net,MVC 2 - 发布视图模型
查看下面的代码。
我的控制器:
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;
}
我的视图模型看起来像这样
namespace ASurvey.ViewModels {
public class QuestionViewModel {
public Question Question { get; set; }
public List<QuestionType> Types { get; set; }
public MultipleChoice Type { get; set; }
}
}
MultipleChoice是否有默认的构造函数,以便MVC可以创建它?你没有发布代码。 – queen3 2010-07-08 14:28:18