gpt4 book ai didi

c# - 可重复使用的复选框部分 View

转载 作者:太空宇宙 更新时间:2023-11-03 15:46:17 24 4
gpt4 key购买 nike

我有一个局部 View ,它使用类 (CheckBoxModel) 的列表 (CheckBoxListModel) 以及字符串和 bool 值来创建复选框列表。该代码用于创建复选框,并在页面发布时将选中的复选框发送回 Controller 。我正试图找到一种方法使我的部分可重用。正如您在代码中看到的那样,我发送了部分完整模型,这可以在页面发布时获取更新的复选框。我试图发送我的模型的 CheckBoxListModel,但它不起作用,因为当它创建复选框时名称不正确。我想通过向它发送一个 CheckBoxListModel 来重用部分,这样我就不必在每次需要一组复选框时都创建一个单独的部分。

我尝试将_CheckBoxListPartial.cshtml 更改为

    @model MySite.Models.ViewModels.CheckBoxListModel
...
@Html.EditorFor(x => x.CheckBoxes)
...

但如果没有 clmReturnOptions,复选框名称最终会以 name="CheckBoxes[0].isChecked"而不是 name="clmReturnOptions.CheckBoxes[0].isChecked"结束,因此当页面发布时它们不会在模型中更新并且返回到 Controller 。

我一直在看:http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/但如果不将整个模型发送到我的部分,似乎仍然无法让复选框工作。

CheckBoxListModel.cs

        public class CheckBoxListModel: ICheckBoxList
{
public IList<CheckBoxModel> CheckBoxes { get; set; }
public string CheckBoxListTitle { get; set; }

public CheckBoxListModel()
{
}
}

public class CheckBoxModel
{
public string CheckBoxName { get; set; }
public string DisplayName { get; set; }
public bool isChecked { get; set; }

public CheckBoxModel()
{ }

public CheckBoxModel(string checkboxname, string displayname, bool ischecked)
{
CheckBoxName = checkboxname;
DisplayName = displayname;
isChecked = ischecked;
}
}

public interface ICheckBoxList
{
IList<CheckBoxModel> CheckBoxes { get; set; }
string CheckBoxListTitle { get; set; }
}

ReportFilterViewModel.cs

        public class ReportFilterViewModel
{
public ReportFilterViewModel()
{
clmReturnOptions = new CheckBoxListModel();
}

public CheckBoxListModel clmReturnOptions { get; set; }
}

filters.cshtml <-- 这是局部调用的地方

    @model MySite.Areas.Reports.Models.ViewModels.ReportFilterViewModel
...
@if (Model.Filters.IsReturnsOptionsAvailable)
{
Html.RenderPartial("_CheckBoxFilterPartial", Model.clmReturnOptions);
}
...

_CheckBoxFilterPartial.cshtml

    @model MySite.Areas.Reports.Models.ViewModels.ICheckBoxList

@{
Layout = null;
}

<!DOCTYPE html>
<html>
<head>
<title>Returns Options</title>
</head>
<body>
<div class="col-md-4">
<div class="plm prm ptm pbm configureCellSplitBG configureCellSplitBG-outline mtm">
<div class="row mlm mrm">
<h6>@Model.CheckBoxListTitle</h6>
</div>
@Html.EditorFor(x => x.CheckBoxes)
</div>
</div>
</body>
</html>

CheckBoxModel.cshtml

    @model MySite.Areas.Reports.Models.ViewModels.CheckBoxModel
<div class="row mlm mrm">
<div class="form-group">
<label class="checkbox">
@Html.CheckBoxFor(x => x.isChecked, new { @data_toggle = "checkbox" })
@Html.LabelFor(x => x.CheckBoxName, Model.DisplayName)
@Html.HiddenFor(x => x.CheckBoxName)
</label>
</div>
</div>

更新当我查看源代码时,我可以看到 CheckBox 名称仍然是:name="CheckBoxes[0].isChecked"因此,当模型返回到 Controller 时,列表为空

我所做的另一项更改是将 CheckBoxListModel.cs 从 MySite.Models.ViewModels 移至 MySite.Areas.Reports.Models,因为其他所有内容都在 reports.models 下。

问题似乎是局部 View 。如果我将 @Html.EditorFor(x => x.clmReturnOptions.CheckBoxes) 放在我的主页中,则会使用全名创建复选框并正确更新。一旦我尝试在局部 View 中使用 EditorFor,复选框名称就会更改,并且指向它们的链接返回到模型中断。我想在局部 View 中使用它,这样我就不必在我想要复选框列表的任何地方添加所有 ui 格式。

我已经更新了上面的代码

最佳答案

您需要将前缀传递给分部 View ,以便正确命名元素

@if (Model.Filters.IsReturnsOptionsAvailable)
{
Html.RenderPartial("_CheckBoxFilterPartial", Model.clmReturnOptions, new ViewDataDictionary
{
TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "clmReturnOptions" }
})
}

您还可以编写一个自定义的 html 帮助器来使这更容易一些

public static MvcHtmlString PartialFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string partialViewName)
{
string name = ExpressionHelper.GetExpressionText(expression);
object model = ModelMetadata.FromLambdaExpression(expression, helper.ViewData).Model;
var viewData = new ViewDataDictionary(helper.ViewData)
{
TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = name }
};
return helper.Partial(partialViewName, model, viewData);
}

并用作

@Html.PartialFor(m => m.clmReturnOptions, "_CheckBoxFilterPartial")

关于c# - 可重复使用的复选框部分 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27967080/

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