Httppost后,Mvc3检查表单值

时间:2013-01-09 18:22:04

标签: asp.net-mvc-3 modelstate

我有2个问题,1。我有一个编辑操作并使用 formcollection 从Httppost上的HiddenFor中获取值,然后我用它在另一个数据库表中进行更改我想知道有没有一种方法可以在表单提交后看到FormCollection的价值?

[HttpPost]
    public ActionResult Edit(relisting house,FormCollection tt)
    {


        if (ModelState.IsValid)
        {


// I would like a way to check the value of getid after submission since 
//my code isn't working. This is an EDIT Action so the GETID has a value 
 //atleast before the post.
            int getid = Convert.ToInt32(tt["RegistrationID"]);
var checksopen = (from d in db.openhouses where getid == d.RegistrationID 
select d).FirstOrDefault();

            house.squarefeet = checksopen.squarefeet;
            house.city = checksopen.city;

            db.Entry(checksopen).State = EntityState.Modified;
            db.SaveChanges();





        }
        return View(house);
    }

我的上一个问题是我上面的完整代码使用了2 db.Entry()。State = EntityState.Modified;但只有1个工作;是否有任何东西可以使第二个实体。修改不起作用?假设 checksopen 确实匹配?我将数据从父模型重新传递到子模型 openhouse ,因此对于每个开放式房间都有一个重新存在,所以我想捕获两者中的更改以保持它同步。

 [HttpPost]
    public ActionResult Edit(relisting house,FormCollection tt)
    {

        if (ModelState.IsValid)
        {

 // This one works
            db.Entry(house).State = EntityState.Modified;
            db.SaveChanges();

// This one doesn't work

            int getid = Convert.ToInt32(Request.Form["RegistrationID"]);

            var checksopen = (from d in db.openhouses where getid == 
    d.RegistrationID select d).FirstOrDefault();

            house.squarefeet = checksopen.squarefeet;
            house.city = checksopen.city;

            db.Entry(checksopen).State = EntityState.Modified;
            db.SaveChanges();





        }
        return View(house);
    }

1 个答案:

答案 0 :(得分:0)

  

我想知道在表单提交后我能看到FormCollection的值吗?

有可能。您的代码可能会将参数传递给您在发布操作后返回的Model属性。 return View(house);您正在返回名为house的模型。只需向此模型添加新的公共参数,然后将RegistrationID分配给它。

提示:出于性能方面的考虑,无需发布FormCollection,因为它会发布您视图中的所有项目。仅发布包含隐藏模型值的必要属性的Model将是更好的方法。

相关问题