1

我的项目是带有 jquery mobile 的 MVC 4。我试图弄清楚如何在提交时使用以下数据填充我的模型:

  1. 来自将在获取请求中填充的隐藏值

  2. 从视图列表中选中单选按钮值

这是我的模型:

 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 属性

4

3 回答 3

2

它实际上最终成为这条线:

 public List<QuestionModel> QuestionList = new List<QuestionModel>();

我需要 get/set 来初始化

    public List<QuestionModel> QuestionList { get; set; }
于 2012-09-24T13:46:24.833 回答
0

请问你能在之后添加代码吗

[AllowAnonymous]
[HttpPost]
public ActionResult Index(ResultsModel model,FormCollection fc)
{
    if (fc["Cancel"] != null)
    {
        return RedirectToAction("Index", "Home");
    }
    if (ModelState.IsValid)
    {

如果您完成代码,这将是一篇好文章。

于 2012-10-12T00:03:24.387 回答
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 => model.Sexe, @radioitem.Value) @Html.Label(@radioitem.Text) }

于 2014-05-13T22:44:25.707 回答