C# 加载带有ID的模型的partialview。BeginForm会返回已编辑的模型,但ID会丢失吗?
我有一个模型:C# 加载带有ID的模型的partialview。BeginForm会返回已编辑的模型,但ID会丢失吗?,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个模型: public class Wellbore { public int Id { get; set; } [DisplayName("Planned Depth")] public double PlannedDepth { get; set; } [DisplayName("Reference Depth")] public double ReferenceDepth { get; set; } [DisplayName("Refe
public class Wellbore
{
public int Id { get; set; }
[DisplayName("Planned Depth")]
public double PlannedDepth { get; set; }
[DisplayName("Reference Depth")]
public double ReferenceDepth { get; set; }
[DisplayName("Reference Point")]
public string ReferencePoint { get; set; }
[DisplayName("Use Section List")]
public bool UseSectionList { get; set; }
[DisplayName("Well Fluid Specific Gravity")]
public double WellFluidSpecificGravity { get; set; }
public List<WellboreSection> WellboreSections { get; set; }
public int WellId { get; set; }
}
还有景色
@using (Html.BeginForm("Wellbore_Edit", "Wellbore", FormMethod.Post, new {@class = "form-horizontal"}))
{
<div class="form-group">
@Html.LabelFor(m => m.Id, new { @class = "col-md-3 control-label" })
<div class="col-md-3">
@Html.TextBoxFor(m => m.Id, new { @class = "form-control", disabled = "disabled" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.ReferenceDepth, new { @class = "col-md-3 control-label" })
<div class="col-md-3">
@Html.TextBoxFor(m => m.ReferenceDepth, new { @class = "form-control" })
</div>
</div>
@使用(Html.BeginForm(“井眼编辑”,“井眼”,FormMethod.Post,new{@class=“form horizontal”}))
{
@LabelFor(m=>m.Id,新的{@class=“col-md-3控制标签”})
@TextBoxFor(m=>m.Id,新的{@class=“formcontrol”,disabled=“disabled”})
@LabelFor(m=>m.ReferenceDepth,新的{@class=“col-md-3控件标签”})
@TextBoxFor(m=>m.ReferenceDepth,new{@class=“form control”})
等等
当我使用控制器中的Index方法加载视图时,一切都正常。对象包含所有适当的值
在视图中完成编辑后,单击“保存”,表单将发回。我在“井眼”参数中收到一个对象,如果我查看它,所有属性都有值,除了Id和WellId
WellId没有在表单中使用,所以我可以理解,但Id是防卫性使用的。为什么它不在返回的对象中?
这些对象是如何创建的?它的内部功能是什么…这是因为disabled=“disabled” 请使用@readonly=“readonly”代替禁用的
@Html.TextBoxFor(m => m.UserName, new {@readonly="readonly"})
禁用输入不回发这是一项功能吗?对我来说,这听起来很奇怪…在我的情况下,我想显示它,但用户不应该能够编辑它,当然,当更新数据库中的实体时,我需要它…那么如何解决这个问题呢?你可以将其设置为只读而不是禁用如果我使用只读,那么它会是吗残疾?我想让“外表”变得残疾…@BjørnØyvindHalvorsen是的,它会很好地工作
@Html.TextBoxFor(m => m.UserName, new {@readonly="readonly"})