【发布时间】:2011-05-25 20:52:21
【问题描述】:
我有这个模型:
public class QuestionSimple
{
public string Body { get; set; }
public bool IsSingleChoice { get; set; }
public List<String> Answers { get; set; }
public string Difficutly { get; set; }
public string Explanation { get; set; }
}
我尝试在Global.asax.cs中使用这一行进行绑定
ModelBinders.Binders.Add(typeof(QuestionSimple), new AddQuestionSimpleBinder());
...有了这个活页夹
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
// Get the raw attempted value from the value provider
string key = bindingContext.ModelName;
ValueProviderResult val = bindingContext.ValueProvider.GetValue(key);
//val is ALWAYS NULL
return null;
}
但 val 始终为空。
这是当我不使用我的活页夹时应该返回(实际上确实返回)答案列表的视图。
@using (Html.BeginForm("AddQuestionSimple", "Topic", FormMethod.Post, new { @id = "mainForm" }))
{
<input type="text" name="questionToBeAdded.Answers[0]" value="ff" />
<input type="text" name="questionToBeAdded.Answers[1]" value="ddds" />
<input type="text" name="questionToBeAdded.Answers[2]" value="ff" />
<input type="text" name="questionToBeAdded.Answers[3]" value="ddds" />
<input type="text" name="questionToBeAdded.Answers[4]" value="ff" />
<input type="text" name="questionToBeAdded.Answers[5]" value="ddds" />
<input value="Add question" type="submit" style="position: static; width: 10em; height: 3em;
font-size: 1em;" />
}
当我发布它们时,默认的模型绑定器确实会获取我的值,但我的 val 始终是 null。
为什么会这样?
(应该提到这是解决this更大问题的尝试)。
编辑 1:
这是应该绑定的动作
[HttpPost]
public ActionResult AddQuestionSimple(PMP.WebUI.Models.UserInteractionEntities.UserInput.QuestionSimple questionToBeAdded)
{
return View("AddQuestion");
}
谢谢。
【问题讨论】:
标签: asp.net-mvc-3