我的项目是带有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; }
}
它有一系列问题,其中包含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>
在上面的每个代码中,我的model.Questionlist集合没有被更新。我还需要弄清楚如何将单选按钮单击(是,否或不适用)绑定到我的model.Questionlist集合的AnswerID属性
答案 0 :(得分:2)
它实际上最终成为了这一行:
public List<QuestionModel> QuestionList = new List<QuestionModel>();
我需要get / set来初始化
public List<QuestionModel> QuestionList { get; set; }
答案 1 :(得分:0)
请在
之后添加代码[AllowAnonymous]
[HttpPost]
public ActionResult Index(ResultsModel model,FormCollection fc)
{
if (fc["Cancel"] != null)
{
return RedirectToAction("Index", "Home");
}
if (ModelState.IsValid)
{
如果您完成代码,这将是一个很好的帖子。
答案 2 :(得分:0)
MVC5使用Razor绑定单选按钮的列表
在程序的任何地方创建一个类RadioList
public class RadioList
{
public int Value { get; set; }
public string Text { get; set; }
public RadioList(int Value, string Text)
{
this.Value = Value;
this.Text = Text;
}
}
在您的控制器
中 List<RadioList> Sexes = new List<RadioList>();
Sexes.Add(new RadioList(1, "Male"));
Sexes.Add(new RadioList(2, "Female"));
Sexes.Add(new RadioList(3, "Other"));
ViewBag.SexeListe = Sexes;
在Razor视图中(用数据库/模型字段中的值替换model.Sexe
@foreach(ViewBag.SexeListe中的RadioList radioitem){
@ Html.RadioButtonFor(model =&gt; model.Sexe,@ radioitem.Value)@ Html.Label(@ radioitem.Text) }