我有以下 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;
}