gpt4 book ai didi

C# MVC 从 View 中获取模型列表

转载 作者:行者123 更新时间:2023-11-30 17:37:51 25 4
gpt4 key购买 nike

我有一个模型列表,它显示在表单的 View 中。如此有效,但是如果我单击“保存”,我会取回模型,但列表为空。有人有想法吗?

查看:

@model AdminFeatureSetsModel

@{
Layout = "~/Views/Shared/_AdministrationLayout.cshtml";
}

@section headerTitle
{
<div>Edit Feature Settings</div>
<br />
<br />
}

@using (Html.BeginForm("Save", "AdminFeatureSets", FormMethod.Post, new { id = "EditFeatureSet", name = "EditFeatureSet", @class = "form-horizontal" }))
{
<div class="grey-border-generic" id="EditFeatureSet">
<div class="col-sm-2">
<b> Feature Name</b>
</div>
<div class="col-sm-2">
<b> Enabled</b>
</div>
<div class="col-sm-2">
<b> Verify</b>
</div>
<div class="col-sm-2">
<b> Enabled By User</b>
</div>
<div class="col-sm-2">
<b>Enabled By Organization </b>
</div>
<br />
<br />
@for (int i = 0; i < Model.FeatureSets.Count; i++)
{

if (Model.FeatureSets[i].IsFeatureInDB == true)
{
<div class="col-sm-2">
<b> @Model.FeatureSets[i].Name</b>
</div>
<div class="col-sm-2">
@Html.TextBoxFor(m => Model.FeatureSets[i].Enabled, new { @class = "form-control", @id = "EditEnable" })
</div>
<div class="col-sm-2">
@Html.TextBoxFor(m => Model.FeatureSets[i].Verify, new { @class = "form-control", @id = "EditVerify" })
</div>
<div class="col-sm-2">
@Html.TextBoxFor(m => Model.FeatureSets[i].EnabledByUser, new { @class = "form-control", @id = "EditEnableByUser" })
</div>
<div class="col-sm-2">
@Html.TextBoxFor(m => Model.FeatureSets[i].EnabledByOrganisation, new { @class = "form-control", @id = "EditEnableByOrganization" })
</div>
<br />
<br />
}
else
{
<div class="col-sm-2">
<b> @Model.FeatureSets[i].Name</b>
</div>
<div class="col-sm-2" style="color:red">
<b> Not in database</b>
</div>
<br />
<br />
}


}

<button class="btn btn-xyzmo-secondary " id="saveEditsBtn">Save</button>
</div>
}

Controller :在 foreach(model.FeatureSets) 保存的函数中,FeatureSets (List) 始终为空。

public ActionResult Index()
{
AdminFeatureSetsModel modelFeatureSets = new AdminFeatureSetsModel();
modelFeatureSets.FeatureSets = new List<FeatureSetModel>();
var featureInDB = DbContext.GetAllEntitiesQueryable<Feature>();

var clazz = typeof(FeatureSet);
foreach (var property in clazz.GetProperties())
{
FeatureSetModel modelFeatureSet = new FeatureSetModel();
var feature = featureInDB.FirstOrDefault(f => f.Name == property.Name);
if (feature != null)
{
modelFeatureSet.Name = feature.Name;
modelFeatureSet.Enabled = feature.Enabled;
modelFeatureSet.Verify = feature.Verify;
modelFeatureSet.EnabledByUser = feature.EnableByUser;
modelFeatureSet.EnabledByOrganisation = feature.EnableByOrganization;
modelFeatureSet.IsFeatureInDB = true;
}else
{
modelFeatureSet.Name = property.Name;
modelFeatureSet.IsFeatureInDB = false;
}
modelFeatureSets.FeatureSets.Add(modelFeatureSet);

}
return View(modelFeatureSets);
}

public ActionResult Save(AdminFeatureSetsModel model)
{

var featureInDB = DbContext.GetAllEntitiesQueryable<Feature>();
foreach (var featureSet in model.FeatureSets) // here the FeatureSet of the model is always empty
{
if (featureSet.IsFeatureInDB == true)
{
var feature = featureInDB.FirstOrDefault(f => f.Name == featureSet.Name);
feature.Enabled = featureSet.Enabled;
feature.Verify = featureSet.Verify;
feature.EnableByUser = featureSet.EnabledByUser;
feature.EnableByOrganization = featureSet.EnabledByOrganisation;
}
}
DbContext.SaveChanges();
return RedirectToAction("Index", "AdminFeatureSet");
}

带有列表的模型:

public class AdminFeatureSetsModel :AdminBaseModel
{
public List<FeatureSetModel> FeatureSets { get; set; }
}

模型特征集:

public class FeatureSetModel : AdminBaseModel
{
public string Name { get; set; }

public bool Enabled { get; set; }
public bool Verify { get; set; }
public bool EnabledByUser { get; set; }
public bool EnabledByOrganisation { get; set; }

public bool IsFeatureInDB { get; set; }

public List<User> EnabledUserList { get;set;}
public List<Organization> EnabledOrginaizationList { get; set; }

}

View of the FeatureSetModels


编辑

     @Html.HiddenFor(m => m.FeatureSets[i].IsFeatureInDB)
if (Model.FeatureSets[i].IsFeatureInDB == true)
{
@*<input type="hidden" name="FeatureSets.Index" value="@i" />*@
<div class="col-sm-2">
<b> @Model.FeatureSets[i].Name</b>
</div>
<div class="col-sm-2">
@Html.CheckBoxFor(m => Model.FeatureSets[i].Enabled)
</div>
<div class="col-sm-2">
@Html.CheckBoxFor(m => Model.FeatureSets[i].Verify)
</div>
<div class="col-sm-2">
@Html.CheckBoxFor(m => Model.FeatureSets[i].EnabledByUser)
</div>
<div class="col-sm-2">
@Html.CheckBoxFor(m => Model.FeatureSets[i].EnabledByOrganisation)
</div>
<br />
<br />
}
else
{
@Model.FeatureSets[i].Name
@Html.HiddenFor(m => m.FeatureSets[i].Enabled)
@Html.HiddenFor(m => m.FeatureSets[i].Verify)
@Html.HiddenFor(m => m.FeatureSets[i].EnabledByUser)
@Html.HiddenFor(m => m.FeatureSets[i].EnabledByOrganisation)
}

最佳答案

它不具有约束力的原因是因为你

if (Model.FeatureSets[i].IsFeatureInDB == true)

代码。如果该值为 false,则不会为该 FeatureSetModel 生成任何表单控件,因此您会回发非连续的索引对象。

默认情况下,DefaultModelBinder 要求集合索引器从零开始并且是连续的。如果集合中的第一项具有 IsFeatureInDB = false,则该集合将为空。 (如果第 2 项是 true,而第 3 项是 false,那么您只会在集合中得到 2 项)

解决此问题的一种方法是在 else block 中为 FeatureSetModel 的每个属性添加隐藏输入。

@for (int i = 0; i < Model.FeatureSets.Count; i++)
{
@Html.HiddenFor(m => m[i].IsFeatureInDB ) // add this so its value posts back
if (Model.FeatureSets[i].IsFeatureInDB == true)
{
@Model.FeatureSets[i].Name
@Html.TextBoxFor(m => m[i].Enabled, new { @class = "form-control" }) // remove id attribute
....
}
else
{
@Model.FeatureSets[i].Name</b>
@Html.HiddenFor(m => m[i].Enabled) // add hidden inputs
....
}

另一种选择,如果您不希望在提交时将具有 IsFeatureInDB = false 的对象包含在集合中,则为集合索引器包含一个隐藏输入,它允许非零/要绑定(bind)的非连续索引器

if (Model.FeatureSets[i].IsFeatureInDB == true)
{
<input type="hidden" name="FeatureSets.Index" value="@i" /> // add this
<div class="col-sm-2">
<b> @Model.FeatureSets[i].Name</b>
</div>
<div class="col-sm-2">
@Html.TextBoxFor(m => Model.FeatureSets[i].Enabled, new { @class = "form-control" }) // remove the invalid id attributes
</div>

另请注意,您需要为 IsFeatureInDB 属性包含一个隐藏输入。没有它,该属性的值在 POST 方法中将始终为 false,并且循环中的代码将不会被执行

旁注:从您的助手中删除 new { @id = "xxx" 代码。重复的 id 属性是无效的 html。

关于C# MVC 从 View 中获取模型列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37337852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com