如何获取新修改的模型对象的导航属性

时间:2013-08-05 10:28:24

标签: asp.net-mvc

我有以下ViewModel类: -

public class RackJoin{
public Rack Rack { get; set; }
public Resource Resource { get; set; }}

以下Post Edit操作方法: -

[HttpPost]
        public ActionResult Edit(RackJoin rj,FormCollection formValues)
        {
            try
            {
                if (ModelState.IsValid)
                {
repository.InsertOrUpdateRack(rj.Rack, User.Identity.Name, rj.Resource.RESOURCEID);
                    repository.Save();
                    return RedirectToAction("Index");
                }

            }
            catch (DbUpdateConcurrencyException ex)
            {
 //code goes here
ViewBag.PossibleDataCenters = repository.AllDataCenter().OrderBy(a => a.Name).ToList();
ViewBag.PossibleZones = repository.AllZone().OrderBy(a => a.Name).ToList();
ViewBag.Products = repository.GetProducts("Rack").OrderBy(a => a.COMPONENTNAME).ToList();

            return View(rj);
 }

以下观点: -

@model TMS.ViewModels.RackJoin

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm()) {
@Html.HiddenFor(model => model.Rack.RackID)
@Html.HiddenFor(model => model.Resource.RESOURCEID)
        @Html.HiddenFor(model => model.Rack.timestamp)
    <div >
    <span class="f">Tag Number </span> 
   <span class="f"> @Html.TextBoxFor(model => model.Rack.Technology.Tag, new 
        { disabled = "disabled", @readonly = "readonly" })</span>
        </div>
        @Html.Partial("_CreateOrEdit", Model)
<input type="submit" value="Save" class="btn btn-primary"/>

但我遇到的问题是我无法在视图中检索修改后的Rack对象的Technology导航属性

<span class="f"> @Html.TextBoxFor(model => model.Rack.Technology.Tag, new 
        { disabled = "disabled", @readonly = "readonly" })</span>

,因为我正在创建一个新的Rack对象并将其标记为已修改?所以我如何解决这个问题。

:: EDIT :: 存储库方法是: -

 public void InsertOrUpdateRack(Rack rack,string username,long assetid)
        {
//code goes here
// Existing entity
tms.Entry(rack).State = EntityState.Modified;
        }

1 个答案:

答案 0 :(得分:0)

如果要将此值发送到服务器,则不应在输入字段上使用disabled属性。 readonly应该足够,并允许在提交表单时将值发送到服务器:

@Html.TextBoxFor(
    model => model.Rack.Technology.Tag, 
    new { @readonly = "readonly" }
)