MVC模型null对象引用未设置为对象的实例

时间:2013-05-07 12:17:13

标签: c# asp.net-mvc model

通过方法发布时遇到问题。我首先调用视图并将模型传递给视图。但是当模型返回并发布时,所有模型都将为null。这是我的代码

此方法成功返回模型并且需要所有项目

public ActionResult RenewFixed(int id)
    { 
        return View(new TransferModel(id, new Service1Client().getIdbyUsername(User.Identity.Name)));
    }

此处模型将所有项目重置为null并发生错误

    [HttpPost]
    public ActionResult RenewFixed(int id, TransferModel model)
    {
        model.myAccount.FixedDate = DateTime.Today;
        model.myAccount.RateId = model.monthFixed;
        new Service1Client().updateAccount(model.myAccount);
        return View("Index");
    }

1 个答案:

答案 0 :(得分:-1)

尝试使用FormCollection。或者,如果您想使用模型,请在您的Model类中将id设置为属性,该类将隐藏在View中。类似的东西:

@Html.HiddenFor("ID")

然后您可以使用model.ID

等模型访问Id