好吧,這讓我感到非常緊張。有時候我發現MVC我在樹林裏迷失了一點,開始往錯誤的方向看去,一切都變得很糟糕。所以我想伸出手去看看一雙新鮮的眼睛是否會指向正確的方向。MVC 5動態數據表 - 未發佈到控制器的模型列表
基本上,結構允許管理員創建自己的問題,這本身就很簡單,但在這種情況下,它不僅僅是一個「單一」答案框。例如:
您商家位置的名稱和地址是什麼?
名稱:_ __ _ __ _地址:_ __ _市:_ __國家:__郵編:_
因此,如果客戶端只有一個位置,它將與5個文本框(名稱,地址,城市,州,郵編)的答案......但由於問題是動態創建的,它可能是3個文本框,1,2,等等
所以我遇到的問題是當模型發佈回控制器視圖模型的問題部分是好的,但答案/選擇部分總是返回null。請看一下,看看是否有巨大的開關切換,我明顯忽略了包括在內。
這裏的模型和視圖模型:
public class Questions
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public int FormID { get; set; }
[Display(Name = "Question Order")]
[Range(0, Int32.MaxValue, ErrorMessage = "Must Use an Integer")]
public int QuestionOrder { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string Question { get; set; }
[Display(Name = "Help Text")]
public string HelpText { get; set; }
public bool IsActive { get; set; }
[Display(Name = "Question May Have More Than One Entry")]
public bool CanHaveMoreThanOne { get; set; }
public int QuestionTypeID { get; set; }
}
public class Choices
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public int QuestionID { get; set; }
public string FieldName { get; set; }
public string QuestionLabel { get; set; }
public bool IsRequired { get; set; }
public string RegEx { get; set; }
public string TextIfSelected { get; set; }
public string QuestionToSkipToIfSelected { get; set; }
public bool IsActive { get; set; }
[NotMapped]
public string AnswerText { get; set; }
[NotMapped]
public bool AnswerBool { get; set; }
[NotMapped]
public int AnswerNumeric { get; set; }
[NotMapped]
public DateTime AnswerDate { get; set; }
}
public class ViewQuestion
{
public Questions Question { get; set; }
public IEnumerable<Choices> Answers { get; set; }
}
這裏的觀點:
@using InterviewMaster.Models
@model ViewQuestion
@using (Html.BeginForm("AnswerQuestion","Interview", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.HiddenFor(m => m.Question.CanHaveMoreThanOne)
@Html.HiddenFor(m => m.Question.FormID)
@Html.HiddenFor(m => m.Question.HelpText)
@Html.HiddenFor(m => m.Question.ID)
@Html.HiddenFor(m => m.Question.Question)
@Html.HiddenFor(m => m.Question.QuestionOrder)
@Html.HiddenFor(m => m.Question.QuestionTypeID)
<h3>@Model.Question.Question</h3>
<hr />
@if (Model.Question.QuestionTypeID == 1)
{
foreach(var o in Model.Answers)
{
Html.RenderPartial("Partial1", o);
}
}
else if (Model.Question.QuestionTypeID == 2)
{
//TO-DO: Make other types of answers such as radio buttons, checkboxes, etc.
}
<input type="submit" value="Create" class="btn btn-default" />
</div>
}
這裏的部分我用
@model InterviewMaster.Models.Choices
@using(Html.BeginCollectionItem("Choices"))
{
@Html.HiddenFor(model => model.ID)
@Html.HiddenFor(model => model.QuestionID)
@Html.HiddenFor(model => model.FieldName)
@Html.HiddenFor(model => model.QuestionLabel)
@Html.HiddenFor(model => model.IsRequired)
@Html.HiddenFor(model => model.RegEx)
@Html.HiddenFor(model => model.TextIfSelected)
@Html.HiddenFor(model => model.QuestionToSkipToIfSelected)
@Html.HiddenFor(model => model.IsActive)
@Html.HiddenFor(model => model.AnswerBool)
@Html.HiddenFor(model => model.AnswerDate)
@Html.HiddenFor(model => model.AnswerNumeric)
<div class="form-group">
<label>@Html.ValueFor(model => model.QuestionLabel)</label>
@Html.TextBoxFor(model => model.AnswerText)
</div>
}
以及最後但並非最不重要的,控制器收到此帖:
// POST: /Interview/AnswerQuestion
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AnswerQuestion(ViewQuestion dto)
{
foreach(var answer in dto.Answers)
{
Console.Write("Answer:" + answer.AnswerText);
}
return RedirectToAction("ContinueInterview", new { OrderID = 1, OrderDetailID = 1, FormID = 1, QuestionID = 1 });
}
您可以將您的答案標記爲正確答案。 –