簡體   English   中英

ASP.Net MVC表單帖子不能綁定模型列表屬性

[英]ASP.Net MVC form post can't bind model list property

我正在嘗試創建一個調查頁面,其中包含文本框和單選按鈕列表或復選框字段。 無論我嘗試什么,我都無法在提交表單時獲得要綁定的model.Questions屬性; 使用null Questions屬性創建模型。

請告訴我你有一些想法可以幫助我!

視圖模型如下所示:

// Survey view model
public class Question
{
    public int Id { set; get; }
    public string QuestionText { set; get; }

    public bool IsHeading { get; set; }
    public bool IsList { get; set; }
    public bool IsCheckBox { get; set; }

    public List<Answer> Answers { set; get; }
    [Required]
    public string SelectedAnswer { set; get; }
    public string GivenAnswer { get; set; }
    public Question()
    {
        Answers = new List<Answer>();
        GivenAnswer = "-1";
        SelectedAnswer = "-1";
        QuestionText = "";
    }
}

public class Answer
{
    public int Id { set; get; }
    public string AnswerText { set; get; }
}

public class Assessment
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int LessonId { get; set; }
    public Module Lesson { get; set; }

    public SurveyType SurveyType { get; set; }

    public virtual List<Question> Questions { set; get; }
    public Assessment()
    {
        Questions = new List<Question>();
    }
}

控制器方法如下所示:

 [HttpPost]
 public ActionResult Survey(Assessment model)
 {
     if (ModelState.IsValid)
     {
         foreach (var q in model.Questions)
         {
             var qId = q.Id;
             var selectedAnswer = q.SelectedAnswer;
             // Save the data 
         }
         return RedirectToAction("ThankYou", new { id = model.Id }); //PRG Pattern
     }
     //to do : reload questions and answers
     return View(model);
 }

Assessment是主要觀點模型。 以下是視圖(Survey.cshtml)和編輯器模板(EditorTemplates / Question.cshtml,每個問題)

Survey.cshtml:

@model ZTTDD.Models.Assessment
@using ZTTDD.Models

<div class="row">
    <div class="col-md-9">
        @using (Html.BeginForm())
        {
            @Html.HiddenFor(m => m.Id)

            @Html.EditorFor(m => m.Questions)

            <input type="submit" />
        }
    </div>
    <div class="col-md-3">
        <p>
            @Html.ActionLink((string)ViewBag.LessonLinkBackText, "Lesson", "Lessons", new { id = Model.Lesson.Id }, null)
        </p>
        <p>
            @Html.ActionLink(linkName, linkMethod, "Lessons", new { id = Model.Lesson.Id }, null)
        </p>
    </div>
</div>

EditorTemplates / Question.cshtml

@model ZTTDD.Models.Question


@if (Model.IsHeading)
{
    <div class="survey-heading">
        <h4>@Model.QuestionText</h4>
    </div>
    <hr/>
}
@if (!Model.IsHeading)
{
    <div class="form-group">
        @Html.HiddenFor(m => m.Id)

        <p> @Model.QuestionText </p>

        @if (Model.IsList)
        {
            <div class="radio">
                @Html.HiddenFor(m => m.GivenAnswer)
                @foreach (var a in Model.Answers)
                {
                    if (Model.IsCheckBox)
                    {
                    <span class="form-horizontal">
                        @Html.CheckBox(String.Format("Questions_{0}__SelectedAnswer", Model.Id), false, new { value = "", name = String.Format("Questions[{0}].SelectedAnswer", Model.Id) })  @Html.Raw(a.AnswerText) &nbsp;&nbsp;
                    </span>
                    }
                    else
                    {
                        @Html.RadioButtonFor(b => b.SelectedAnswer, a.Id)  @Html.Raw(a.AnswerText) <br />
                    }
                }
            </div>
        }
        @if (!Model.IsList)
        {
            Model.GivenAnswer = "";
            @Html.HiddenFor(m => m.SelectedAnswer)
            @Html.TextAreaFor(m => m.GivenAnswer)
        }
    </div>
}

以下是發布的密鑰列表:

    [0] "Id"    string
    [1] "Questions[1].Id"   string
    [2] "Questions[1].GivenAnswer"  string
    [3] "Questions[1].SelectedAnswer"   string
    [4] "Questions[2].Id"   string
    [5] "Questions[2].GivenAnswer"  string
    [6] "Questions[2].SelectedAnswer"   string
    [7] "Questions[3].Id"   string
    [8] "Questions[3].GivenAnswer"  string
    [9] "Questions[3].SelectedAnswer"   string
    [10]    "Questions[5].Id"   string
    [11]    "Questions[5].GivenAnswer"  string
    [12]    "Questions[5].SelectedAnswer"   string
    [13]    "Questions[6].Id"   string
    [14]    "Questions[6].GivenAnswer"  string
    [15]    "Questions[6].SelectedAnswer"   string
    [16]    "Questions[7].Id"   string
    [17]    "Questions[7].GivenAnswer"  string
    [18]    "Questions[7].SelectedAnswer"   string
    [19]    "Questions[9].Id"   string
    [20]    "Questions[9].SelectedAnswer"   string
    [21]    "Questions[9].GivenAnswer"  string
    [22]    "Questions[10].Id"  string
    [23]    "Questions[10].SelectedAnswer"  string
    [24]    "Questions[10].GivenAnswer" string

更新:

稍微澄清一下:一個Question對象可以表示一個標題(沒有關聯的字段)或輸入(單選按鈕,復選框或textarea)。 據我所知,我需要的值正確發布。 我將嘗試從調試器中獲取它們並在此處發布。

更新:

下面的查詢字符串來自Request.Form.ToString(),具有url解碼和格式化以便於閱讀。 正如您所看到的,值是發布的,但由於某種原因,不會受到Assessment.Questions的約束。 這實際上是由於問題[xx]中的xx值,因為它不是索引值,而是實際的Id?

Id=1&
Questions[1].Id=2&
Questions[1].GivenAnswer=-1&
Questions[1].SelectedAnswer=2&
Questions[2].Id=3&
Questions[2].GivenAnswer=-1&
Questions[2].SelectedAnswer=5&
Questions[3].Id=5&
Questions[3].GivenAnswer=-1&
Questions[3].SelectedAnswer=8&
Questions[5].Id=7&
Questions[5].GivenAnswer=-1&
Questions[5].SelectedAnswer=12&
Questions[6].Id=8&
Questions[6].GivenAnswer=-1&
Questions[6].SelectedAnswer=15&
Questions[7].Id=9&
Questions[7].GivenAnswer=-1&
Questions[7].SelectedAnswer=18&
Questions[9].Id=11&
Questions[9].SelectedAnswer=-1&
Questions[9].GivenAnswer=sdfg sdfg dsfg&
Questions[10].Id=12&
Questions[10].SelectedAnswer=-1&
Questions[10].GivenAnswer=sdfg dfsg sdfg

最后更新:

問題在於,我沒有將任何內容發回服務器以查找標題的問題,因此問題索引實際上是不完整的。 為每個標題添加隱藏的Id字段可以解決問題。

我設置了一個基本項目,利用您發布的代碼示例,一些基本的虛擬數據,以及問題集合。

我猜那時候您正在渲染的特定調查的數據中存在導致問題的內容。

查看代碼,我可以看到兩個可能的問題:

  • Question.IsHeader為true時,我認為沒有任何內容會被發回到此問題的表單中。 我想那時模型活頁夾沒有為這個問題拿起任何東西。 從你的表單數據來看,它看起來就是這樣 - 它缺少0,4和8的索引。我實際上並不知道這是否會導致模型綁定器失敗,但它可以做到。 你可以嘗試把一個HiddenFor的問題ID,在問題編輯模板,其中的部分IsHeading是真實的。
  • 您正在使用的@Html.CheckBox代碼使用問題的ID格式化輸入值,而不是列表中問題的索引。 我猜這也會破壞綁定。

希望如果您能解決這些問題,綁定將起作用。 我建議在沒有任何標題問題的情況下進行調查,並且沒有任何復選框或單選按鈕問題,並查看綁定是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM