私のプロジェクトは、jquery mobile を使用した MVC 4 です。次のようにして、送信時にモデルにデータを入力する方法を見つけようとしています。
get リクエストで入力される非表示の値から
ビューのリストからチェックされたラジオボタンの値
ここに私のモデルがあります:
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; }
}
get 要求に関するデータで満たされた質問のコレクションがあります。コントローラーのコードは次のとおりです。
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>
上記の for each コードでは、私の model.Questionlist コレクションは更新されていません。また、ラジオ ボタンのクリック (はい、いいえ、または適用外) をモデルの AnswerID プロパティに関連付ける方法を理解する必要もあります。Questionlist コレクション