【发布时间】:2014-06-08 23:14:07
【问题描述】:
控制器 httpGet 操作:
[HttpGet]
public ActionResult CalculCom(int id)
{
CalcCom calc = new CalcCom();
ComModel com = calc.CalculCom(id, 10, 2011);
return View(com);
}
这是我的观点:
// Some code to show my model attribute
@using (Html.BeginForm()) {
@Html.HiddenFor(m=>m.ben)
@Html.HiddenFor(m=>m.centre)
@Html.HiddenFor(m=>m.mtn_brut)
@Html.HiddenFor(m=>m.mtn_net)
@Html.HiddenFor(m=>m.mtn_rs)
@Html.HiddenFor(m=>m.quitaList) // this is a list
@Html.HiddenFor(m=>m.val_rs)
@Html.HiddenFor(m=>m.versNum)
<input type="submit" value="Valider"/>
}
这是我的控制器 HttpPost 操作:
[HttpPost]
public ActionResult CalculCom(ComModel model)
{
//some code
foreach (ComModel.quita qi in model.quitaList)
{
var quita = db.QUITA.Find(qi.n_quita);
quita.VERSNUM = vers.VERSNUM;
db.Entry(quita).State = EntityState.Modified;
}
db.SaveChanges();
return RedirectToAction("Index");
}
ComModel :
public class ComModel
{
public int versNum { get; set; }
public decimal mtn_brut { get; set; }
public decimal mtn_net { get; set; }
public decimal mtn_rs { get; set; }
public decimal val_rs { get; set; }
public string ben { get; set; }
public int centre { get; set; }
public List<quita> quitaList = new List<quita>();
public struct quita
{
public decimal mtn_com { get; set; }
public decimal mtn_net { get; set; }
public decimal mtn_ttc { get; set; }
public decimal comp_prime { get; set; }
public decimal mtn_fq { get; set; }
public decimal mtn_tot { get; set; }
public int n_quita { get; set; }
}
}
所以我想做的是:
- 获取模型(HttpGet 操作)
- 显示我的模型,然后用户将决定是否验证模型。
- 如果模型有效,那么我会将其保存到数据库中。
用户点击提交按钮时的问题,模型已发送,但quitaList 中的项目丢失,发送后,在我的HttpPost 控制器中,quitaList 为空
那么如何解决呢?还有其他方法可以做到这一点,在将其保存到数据库之前显示模态,而不是使用 From 发送模型?
【问题讨论】:
-
你能不能也展示一下 ComModel 的代码?
-
@Fresh 请查看更新
标签: c# asp.net asp.net-mvc asp.net-mvc-4