Asp.net 编辑器模板返回空值
我已经基于本文创建了一个编辑器模板 在我按下提交按钮之前,一切正常。我发现我的POST函数返回模型是空的,就像模型没有绑定到视图一样。我一直在尝试我从互联网上知道和发现的所有技巧,但我仍然无法修复它 这是我的控制器Asp.net 编辑器模板返回空值,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,我已经基于本文创建了一个编辑器模板 在我按下提交按钮之前,一切正常。我发现我的POST函数返回模型是空的,就像模型没有绑定到视图一样。我一直在尝试我从互联网上知道和发现的所有技巧,但我仍然无法修复它 这是我的控制器 // GET: /Asset/New public ActionResult New() { ViewBag.typeID = new SelectList(db.Ref_Asset_Types, "ID", "name");
// GET: /Asset/New
public ActionResult New()
{
ViewBag.typeID = new SelectList(db.Ref_Asset_Types, "ID", "name");
return View(new AssetViewModel());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult New(AssetViewModel vm)
// vm.asset should contain new value but currently return null
{
if (ModelState.IsValid)
{
db.Assets.Add(vm.asset);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.typeID = new SelectList(db.Ref_Asset_Types, "ID", "name", vm.asset.typeID);
return View("New", vm);
}
这是我的看法
@using (Html.BeginForm("New","Asset","POST")) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.EditorFor(m=>m.asset, "InputTemplate" )
//注意:如果我不使用自己的模板==>@Html.EditorFor(m=>m.asset),代码就可以工作
这是我的模型
public class Asset
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[HiddenInput(DisplayValue = false)]
public int ID { get; set; }
[DisplayName("No. Siri")]
[StringLength(45)]
public string serial_num { get; set; }
[DisplayName("Model")]
[Required(ErrorMessage = "Model perlu diisi!")]
[StringLength(45)]
public string model { get; set; }
[DisplayName("Harga Seunit")]
[RegularExpression(@"^\d{0,6}(\.\d{2})?$", ErrorMessage = "Sila gunakan format harga yang betul.")]
public float? unit_cost { get; set; }
[UIHint("DropDown")]
[DisplayName("Jenis Aset")]
[Required(ErrorMessage = "Jenis aset perlu dipilih!")]
[DisplayFormat(NullDisplayText = "Belum didaftar")]
public int? typeID { get; set; }
public virtual Ref_Asset_Type type { get; set; }
}
很抱歉给你们添麻烦。。我想我解决了 我最大的错误是使用保留字“model”和“type”作为我的属性名。这可能会导致asp.net在使用用户定义编辑器模板解释我的模型时出现问题 一旦我将属性名称-model更改为model\u name,将类型更改为asset\u type,我就可以在返回模型中看到我的条目了 多谢大家
。。。。为了这个愚蠢的错误花了整整一天一夜,但所学到的教训是值得的当您查看源代码时,编辑器模板生成的输出是什么样子的?粗略地看一下,我建议反射的属性名称与模型绑定约定不匹配您的
EditorTemplate
正在呈现属性资产的控件。您尚未显示此类,但假设它是类型为Asset
且包含属性ID
,则关联的控件将具有name=“ID”
,但您的POST方法参数是AssetViewModel
,这意味着控件必须是name=“Asset.ID”
。你想用这段代码实现什么,而这是用标准的编辑模板所不能实现的?@Stephen Muecke-我在这里尝试的是创建一个可供任何模型使用的模板。因此,我可以在所有“添加”和“编辑”视图中保持相同的样式。顺便说一句,我尝试发送模型而不是viewmodel,但我无法将模型传递到我的模板。(此viewmodel遵循Dino文章的建议)我添加了viewmodel和model以供参考@Brent Mannering-在我看来,源代码实际上与我使用默认模板=>时的源代码相同,输入框将具有id=asset\u property\u name和name=property_name@bapak71,这是真正的代码吗(如果视图模型只包含一个属性,即域模型,那么它为什么会有视图模型)?检查正在生成的html-控件上的属性是name=“asset.ID”
还是name=“ID”
?
@inherits System.Web.Mvc.WebViewPage
@if (Model == null)
{
<span>@ViewData.ModelMetadata.NullDisplayText</span>
}
else
{
foreach (var prop in ViewData
.ModelMetadata
.Properties
.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm)))
{
if (prop.DisplayName != null) { // only display prop not of ComplexType
// note : using bootstrap for css styling
<div class="form-group col-xs-6">
<label class="col-xs-4 control-label text-right">
<span style="color:red"> @(prop.IsRequired ? "*" : "") </span>
<span>@prop.GetDisplayName()</span>
</label>
<div class="col-xs-8">
@if(prop.IsReadOnly)
{
<span class="readonly-field">@Html.Display(prop.PropertyName)</span>
}
else if (prop.TemplateHint == "DropDown")
{
<span>@Html.DropDownList(prop.PropertyName,(IEnumerable<SelectListItem>) ViewData[prop.PropertyName], new { @class = "form-control" })</span>
<span>@Html.ValidationMessage(prop.PropertyName)</span>
}
else
{
<div class="editor-field">
<span>@Html.Editor(prop.PropertyName, new { @class = "text-box single-line form-control" })</span>
<span>@Html.ValidationMessage(prop.PropertyName, new { @class = "label-danger" } )</span>
</div>
}
</div>
</div>
} // if
} // foreach
}
using System;
using SIGMA.Models;
namespace SIGMA.ViewModels
{
public class AssetViewModel
{
public AssetViewModel()
{
asset = new Asset();
}
public Asset asset { get; set; }
}
}
public class Asset
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[HiddenInput(DisplayValue = false)]
public int ID { get; set; }
[DisplayName("No. Siri")]
[StringLength(45)]
public string serial_num { get; set; }
[DisplayName("Model")]
[Required(ErrorMessage = "Model perlu diisi!")]
[StringLength(45)]
public string model { get; set; }
[DisplayName("Harga Seunit")]
[RegularExpression(@"^\d{0,6}(\.\d{2})?$", ErrorMessage = "Sila gunakan format harga yang betul.")]
public float? unit_cost { get; set; }
[UIHint("DropDown")]
[DisplayName("Jenis Aset")]
[Required(ErrorMessage = "Jenis aset perlu dipilih!")]
[DisplayFormat(NullDisplayText = "Belum didaftar")]
public int? typeID { get; set; }
public virtual Ref_Asset_Type type { get; set; }
}