0

我有以下 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 对象的技术导航属性

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

1 回答 1

0

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

@Html.TextBoxFor(
    model => model.Rack.Technology.Tag, 
    new { @readonly = "readonly" }
)
于 2013-08-05T10:33:31.737 回答