我正在尝试创建一个结合了三个HiddenFor帮助程序的HTML扩展。 我设法得到了以下,至少编译:
public static class BatchHelper {
public static MvcHtmlString DisplayHiddenSummary<TModel, TValue>(this HtmlHelper<TModel> helper, BatchSummary batchSummary) where TModel : BatchSummary{
var batchId = helper.HiddenFor(model => model.BatchId);
var batchTotal = helper.HiddenFor(model => model.BatchTotal);
var totalExpected = helper.HiddenFor(model => model.TotalExpected);
var result = MvcHtmlString.Create(batchId.ToString() + batchTotal.ToString() + totalExpected.ToString());
return result;
}
}
我在使用它时遇到了问题,我收到警告说需要参数,这确实很有意义,虽然我看过的所有示例似乎都认为它是一种扩展方法,并且不需要HtmlHelper参数。
谢谢!
EDIT 视图代码如下:
@using GridMvc.Html
@using GridMvc.Sorting
@using MyProj.Web.ViewModels.Receipt
@model MyProj.Web.ViewModels.Grids.DonorSummaryGrid
@Html.AntiForgeryToken()
@if (Model.BatchSummary != null) {
// These Work fine, and are what I'm trying to put in the extension.
@Html.HiddenFor(model => model.BatchSummary.BatchId)
@Html.HiddenFor(model => model.BatchSummary.BatchTotal)
@Html.HiddenFor(model => model.BatchSummary.TotalExpected)
// this is where I want to use the extension
@(Html.DisplayHiddenSummary(Model.BatchSummary))
}
答案 0 :(得分:1)
您的功能应如下所示。 TValue参数是不必要的。另外,你的lambdas不正确。
public static class BatchHelper {
public static MvcHtmlString DisplayHiddenSummary<TModel,TProperty>(this HtmlHelper<TModel> helper, TProperty batchSummary)
where TModel : MyViewModel
where TProperty: BatchSummary
{
var batchId = helper.HiddenFor(a => batchSummary.BatchId);
var batchTotal = helper.HiddenFor(a => batchSummary.BatchTotal);
var totalExpected = helper.HiddenFor(a => batchSummary.TotalExpected);
var result = MvcHtmlString.Create(batchId.ToString() + batchTotal.ToString() + totalExpected.ToString());
return result;
} }
你的Razor应如下所示,假设类型为MyViewModel的强类型视图和名为BatchSummary的属性:
@(Html.DisplayHiddenSummary<MyViewModel, BatchSummary>(Model.BatchSummary))
答案 1 :(得分:1)
你可能没有正确地调用它。你正在使它通用,但你有BatchSummary硬编码。所以...通用部分是不必要的(尽管你可以使用它或传入类似于其他帮助器的表达式)。为此,您可以将其简化为:
public static MvcHtmlString DisplayHiddenSummary(this HtmlHelper helper, BatchSummary batchSummary)
{
var batchId = helper.HiddenFor(model => model.BatchId);
var batchTotal = helper.HiddenFor(model => model.BatchTotal);
var totalExpected = helper.HiddenFor(model => model.TotalExpected);
var result = MvcHtmlString.Create(batchId.ToString() + batchTotal.ToString() + totalExpected.ToString());
return result;
}
然后在视图中:
@Html.DisplayHiddenSummary(Model.BatchSummary)
您可能必须使用@using指令添加此帮助程序的命名空间,将其添加到web.config中,或者将其添加到System.Web.Mvc.Html中。
您可能还会考虑显示和编辑器模板,而不是此自定义帮助程序。
答案 2 :(得分:0)
最终得到了一些有用的东西。在MVC源代码中有一个rumage,并找到了一个片段,帮助我提出以下内容:
using System.Linq.Expressions;
using System.Text;
using StJudes_Donor.Web.ViewModels.Receipt;
namespace System.Web.Mvc.Html {
public static class BatchHelper {
public static MvcHtmlString DisplayHiddenSummary(this HtmlHelper helper, BatchSummary batchSummary) {
StringBuilder batchId = HiddenBuilder(batchSummary.BatchId.ToString(), "BatchId");
StringBuilder batchTotal = HiddenBuilder(batchSummary.BatchTotal.ToString(), "BatchTotal");
StringBuilder batchExpected = HiddenBuilder(batchSummary.TotalExpected.ToString(), "BatchExpected");
return MvcHtmlString.Create(batchId.ToString() + batchTotal.ToString() + batchExpected.ToString());
}
private static StringBuilder HiddenBuilder(String value, String id) {
StringBuilder inputItemBuilder = new StringBuilder();
TagBuilder hiddenInput = new TagBuilder("input");
hiddenInput.MergeAttribute("type", HtmlHelper.GetInputTypeString(InputType.Hidden));
hiddenInput.MergeAttribute("id", id);
hiddenInput.MergeAttribute("name", id);
hiddenInput.MergeAttribute("value", value);
inputItemBuilder.Append(hiddenInput.ToString(TagRenderMode.SelfClosing));
return inputItemBuilder;
}
}
}
在视图中调用
@Html.DisplayHiddenSummary(Model.BatchSummary)
这产生了以下HTML
<input id="BatchId" name="BatchId" type="hidden" value="1">
<input id="BatchTotal" name="BatchTotal" type="hidden" value="21.31">
<input id="BatchExpected" name="BatchExpected" type="hidden" value="12.31">