我知道你在想什么,但是听我说。
我有一个模特:
public partial class DealerBudget
{
public int DealerBudgetID { get; set; }
public int DealerID { get; set; }
public int BudgetYr { get; set; }
public int BudgetMonth { get; set; }
public decimal BudgetAmt { get; set; }
public bool Confirmed { get; set; }
public short BudgetTypeID { get; set; }
}
这些列表在vm中:
public class DealerBudgetVM
{
public List<CNHi.StMarys.ND.DBP.Web.Models.DealerBudget> YrBudget { get; set; }
}
他们填写了Get:
public ActionResult Add()
{
DealerBudgetVM result = new DealerBudgetVM(); //initialised to have all 24 months
List<DealerBudget> dbList = db.DealerBudgets.Where(x => x.DealerID == dbp.dealerID && x.BudgetTypeID == (short)2 && (x.BudgetYr == DateTime.Now.Year || x.BudgetYr == DateTime.Now.Year + 1)).ToList();
//use the data from the DB if it exists
foreach (DealerBudget bud in result.YrBudget)
{
foreach (DealerBudget budDB in dbList)
{
bud.DealerID = dbp.dealerID;
bud.BudgetTypeID = 2; //Service
if (budDB.BudgetYr == bud.BudgetYr && budDB.BudgetMonth == bud.BudgetMonth)
{
bud.DealerBudgetID = budDB.DealerBudgetID;
bud.BudgetAmt = budDB.BudgetAmt;
bud.Confirmed = budDB.Confirmed;
}
}
}
return PartialView(result);
}
并在视图中显示。如果每个月的值已确认,则文本框将被禁用:
@for( int i = 0; i < 12; ++i)
{
@Html.HiddenFor(model => model.YrBudget[i].DealerBudgetID)
@Html.HiddenFor(model => model.YrBudget[i].BudgetTypeID)
@Html.HiddenFor(model => model.YrBudget[i].DealerID)
@Html.HiddenFor(model => model.YrBudget[i].BudgetYr)
@Html.HiddenFor(model => model.YrBudget[i].BudgetMonth)
@Html.HiddenFor(model => model.YrBudget[i].Confirmed)
<div class="pure-u-1-3 pure-u-md-1-6 pure-u-lg-1-12 s-box">
<span class="pure-u-1">@Html.DisplayFor(model => model.YrBudget[i].MonthDesc)</span>
@Html.TextBoxFor(model => model.YrBudget[i].BudgetAmt, (Model.YrBudget[i].Confirmed == false) ? (object)new {type="number", Value=Model.YrBudget[i].BudgetAmt.ToString("0.00")} : (object)new {disabled = "disabled", Value=Model.YrBudget[i].BudgetAmt.ToString("0.00")} )
</div>
}
用户输入新的预算数字并保存:
[HttpPost]
public ActionResult Added(DealerBudgetVM vm)
{
if (ModelState.IsValid)
{
this.ModelState.Clear();
foreach (DealerBudget budVM in vm.YrBudget.Where(x => x.Confirmed == false && x.BudgetAmt > 0).ToList())
{
//If it's not in the DB, add it.
if (budVM.DealerBudgetID == 0)
{
DealerBudget budNew = new DealerBudget { BudgetYr = budVM.BudgetYr, DealerID = budVM.DealerID, BudgetMonth = budVM.BudgetMonth, BudgetTypeID = budVM.BudgetTypeID, BudgetAmt = budVM.BudgetAmt };
if (budNew.BudgetAmt > 0)
{
budNew.Confirmed = true;
budVM.Confirmed = true;
}
db.DealerBudgets.Add(budNew);
db.SaveChanges();
budVM.DealerBudgetID = budNew.DealerBudgetID;
}
else
{
//update and confirm
DealerBudget budDB = db.DealerBudgets.Where(x => x.DealerBudgetID == budVM.DealerBudgetID).FirstOrDefault();
if (budDB == null)
{
}
else
{
budDB.BudgetAmt = budVM.BudgetAmt;
budDB.Confirmed = true;
db.SaveChanges();
budVM.Confirmed = true;
}
}
}
return RedirectToAction("Index", "ServicePerformance");
}
else
{
return PartialView(vm);
}
}
......并且视图没有任何变化。新的(已确认的)预算文本框应该被禁用,而且它们不是。
&#34;没问题!&#34;你说。 &#34;这家伙只需......&#34;
在发布并保存之后,后续的Get将所有正确保存的值发送到VM中的View,但仍会显示旧的(即用户输入的值已确认flag = false)。
如果用户然后转到另一个页面然后返回,则页面将按预期显示。
我在这里(以及其他地方)看到的所有帖子都自信地宣称上述四种解决方案中的一种将解决它。我现在浪费了一天的时间,撞在墙上试图禁用一个愚蠢的文本框。
还有其他想法吗?是因为嵌套或部分视图还是什么?