我的项目是带有 jquery mobile 的 MVC 4。我试图弄清楚如何在提交时使用以下数据填充我的模型:
来自将在获取请求中填充的隐藏值
从视图列表中选中单选按钮值
这是我的模型:
public class ResultsModel
{
[Display(Name = "PatientFirstName")]
public string PatientFirstName { get; set; }
[Display(Name = "PatientLastName")]
public string PatientLastName { get; set; }
[Display(Name = "PatientMI")]
public string PatientMI { get; set; }
public List<QuestionModel> QuestionList = new List<QuestionModel>();
}
public class QuestionModel
{
public string Question { get; set; }
public int QuestionID { get; set; }
public int AnswerID { get; set; }
}
它有一组问题,其中包含有关获取请求的数据。这是控制器代码:
public class ResultsController : Controller
{
//
// GET: /Results/
public ActionResult Index()
{
if (Request.IsAuthenticated)
{
ResultsModel resultsModel = new ResultsModel();
//Get data from webservice
myWebService.TestForm inrform;
var service = new myWebService.myService();
testform = service.TestForm(id);
if (testform != null)
{
//Render the data into results data model
int count = 1;
string text = string.Empty;
foreach (myWebService.Question questiontext in testform.QuestionList)
{
QuestionModel newquestion = new QuestionModel();
text = "Question " + count + ": " + questiontext.text;
if (questiontext.text != null)
{
newquestion.Question = text;
newquestion.QuestionID = questiontext.id;
}
resultsModel.QuestionList.Add(newquestion);
count += 1;
}
}
else
{
//Error
}
return View(resultsModel);
}
// Error
return View();
}
[AllowAnonymous]
[HttpPost]
public ActionResult Index(ResultsModel model,FormCollection fc)
{
if (fc["Cancel"] != null)
{
return RedirectToAction("Index", "Home");
}
if (ModelState.IsValid)
{
在 post 操作中,model.QuestionList 始终为空。这是我的观点:
@model Models.ResultsModel
@{
ViewBag.Title = Resources.Account.Results.Title;
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<li data-role="fieldcontain">
@Html.LabelFor(m => m.INRTestDate)
@Html.EditorFor(m => m.INRTestDate)
@Html.ValidationMessageFor(model => model.INRTestDate)
</li>
<li data-role="fieldcontain">
@Html.LabelFor(m => m.INRValue)
@Html.TextBoxFor(m => m.INRValue)
</li>
<li>
@for (int i = 0; i < Model.QuestionList.Count; i++)
{
<div data-role="label" name="question" >
@Model.QuestionList[i].Question</div>
@Html.HiddenFor(m => m.QuestionList[i].QuestionID)
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal" data-role="fieldcontain" >
<input id="capture_type_yes" name="answer_type" type="radio" data-theme="c" value="1" />
<label for="capture_type_yes" >
Yes</label>
<input id="capture_type_no" name="answer_type" type="radio" data-theme="c" value="0" />
<label for="capture_type_no">
No</label>
<input id="capture_type_na" name="answer_type" type="radio" data-theme="c" value="2" />
<label for="capture_type_na">
Not Applicable</label>
</fieldset>
</div> }
<label for="textarea-a">
Comments</label>
<textarea name="textarea" id="textarea-a"> </textarea>
<fieldset class="ui-grid-a">
<div class="ui-block-a">
<button type="submit" name="Submit" data-theme="c">Submit</button></div>
<div class="ui-block-b">
<button type="submit" name="Cancel" data-theme="b" class="cancel">Cancel</button></div>
</fieldset>
</li>
在上面的每个代码中,我的 model.Questionlist 集合没有被更新。我还需要弄清楚如何将单选按钮单击(是、否或不适用)绑定到我的 model.Questionlist 集合的 AnswerID 属性