我在我的项目中使用MVC框架并使用模型绑定来填充视图,然后使用两个控制器从视图中检索值。
我能够使用模型属性填充视图,但是当我尝试在单击提交按钮时检索值时。该模型返回null属性。
这是ViewModels:
public partial class QuestionAnswer
{
public string QuestionText { get; set; }
public string QuestionId { get; set; }
public string AnswerText { get; set; }
public string SectionName { get; set; }
public bool IsChecked { get; set; }
}
public partial class pluralQuestionAnswer
{
public Dictionary<int, QuestionAnswer> QnAs { get; set; }
public Dictionary<string, int> SectionNameWithRank { get; set; }
public Dictionary<string, int> SectionNameWithQuestionCount { get; set; }
}
以下是控制器:
namespace Temp.Controllers
{
public partial class AIPController : Controller
{
public ActionResult FirstInitiative()
{
pluralQuestionAnswer ViewModel = new pluralQuestionAnswer();
//code to fill the model ViewModel
return View(ViewModel);
}
[HttpPost]
public ActionResult SaveSelectedInitiatives(pluralQuestionAnswer ViewModel, string Save)
{
//some code that uses ViewModel
//this is where ViewModel shows all properties as null
return View();
}
}
}
FirstInitiative
用于通过传递viewModel生成视图,SaveSelectedInitiatives
用于从视图中检索的viewModel中获取值。
以下是视图:
@using Models;
@model pluralQuestionAnswer
@{
ViewBag.Title = "FirstInitiative";
int sectionIterator;
int sectionPracticeIterator;
int totalPracticeIterator = 1;
}
<header></header>
<section>
<header>
<h1>First Initiative</h1>
</header>
@using (Html.BeginForm("SaveSelectedInitiatives", "AIP", FormMethod.Post))
{
<div class="inititive-table" style="overflow:auto">
<div class="inititive-table-header">
<div class="initiative-questioncolumn">
Common Minimum Practices
</div>
<div class="initiative-answercolumn">
Your response
</div>
</div>
@for (sectionIterator = 1; sectionIterator <= Model.SectionNameWithRank.Count; sectionIterator++)
{
<div class="initiative-section">
<div class="initiative-section-name">
<span>@Model.SectionNameWithRank.Keys.ElementAt(sectionIterator - 1)</span>
</div>
@for (sectionPracticeIterator = 1; sectionPracticeIterator <= Model.SectionNameWithQuestionCount[Model.SectionNameWithRank.Keys.ElementAt(sectionIterator - 1)]; sectionPracticeIterator++)
{
<div class="initiative-section-question initiative-questioncolumn">
<label>
@*model binding*@
@Html.HiddenFor(x => x.QnAs[totalPracticeIterator].QuestionId)
<span style="width: 20px; float:left">@Html.CheckBoxFor(x => x.QnAs[totalPracticeIterator].IsChecked)</span>
<span style="display: block; margin-left: 20px">@Html.Raw(Model.QnAs[totalPracticeIterator].QuestionText)</span>
</label><br />
</div>
<div class="initiative-section-answers initiative-answercolumn">
@Model.QnAs[totalPracticeIterator].AnswerText
</div>
{totalPracticeIterator += 1;}
}
</div>
}
</div>
<div>
<input type="submit" class="zs-left zs-button zs-button-action zs-atp-button" name="Save" value="Save" />
</div>
}
</section>
答案 0 :(得分:0)
您可能希望重命名inputs
以匹配模型的KeyValuePair架构,因此请在下面替换:
@Html.HiddenFor(x => x.QnAs[totalPracticeIterator].QuestionId)
使用
@Html.Hidden(string.Format("QnAs[{0}].Key", i), /*<You Need Key's value here> Like: (_kvp.Key) */)
@Html.Hidden(string.Format("QnAs[{0}].Value.QuestionId", i), x.QnAs[/*<You Need Key's value here> Like: (_kvp.Key) */].QuestionId)
您可以尝试在代码下方获取KeyValuePair:
var _kvp = Model.QnAs.ElementAt(totalPracticeIterator)
因此,来自浏览器的发布数据如下所示:
QnAs[0].Key = 1
QnAs[0].Value.QuestionId = 123
QnAs[1].Key = 32
QnAs[1].Value.QuestionId = 55
答案 1 :(得分:0)
在您的词典对象中,将密钥从int
类型更改为string
类型,这应该有效。