简体   繁体   English

使用具有ID的模型加载局部视图。 BeginForm返回已编辑的模型,但是ID丢失了吗?

[英]Loading a partialview with a model that has an ID. BeginForm returns the edited model, but the ID is lost?

I have a model: 我有一个模型:

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; }
}

And i have a controller 我有一个控制器

    public ActionResult Index(int wellId)
    {
        var wellbore = _service.FindBy(x => x.Id == wellId).SingleOrDefault();
        return PartialView("~/Views/WellManagement/Wellbore.cshtml", wellbore);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public void Wellbore_Edit([DataSourceRequest] DataSourceRequest request, Wellbore wellbore)
    {
        if (wellbore != null && ModelState.IsValid)
        {
            wellbore = _service.Edit(wellbore);
        }
    }

And the view.... 还有风景...

@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>

And so on.... 等等....

When i load the view with the Index method in the controller, everything works fine. 当我在控制器中使用Index方法加载视图时,一切正常。 The object contains all of the appropriate values. 该对象包含所有适当的值。

When i have done my editing in the view, i click save, and the form posts back. 当我在视图中完成编辑后,单击“保存”,然后表格回发。 I recieve an object in the Wellbore argument, and if i look at it, all of the properties has values, except Id and WellId.... 我在Wellbore参数中收到一个对象,如果我看一下,则所有属性都具有值,但Id和WellId除外。

WellId is not used in the form, so this i might understand, but Id is defenitly used. WellId不在表单中使用,所以我可能会理解,但是Id被明确使用。 How come this is is not in the object on it's way back? 为什么这不是在返回的对象上呢? How does these objects get created? 如何创建这些对象? Whats the internal function of this.... 这是什么内部功能...

这是因为disabled =“ disabled”请使用@ readonly =“ readonly”代替disabled

@Html.TextBoxFor(m => m.UserName, new {@readonly="readonly"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM