I have 2 question, 1. I have an edit action and use a formcollection to get the value out of a HiddenFor on Httppost which then i use to make a change in another database table and I was wondering is there a way in which I can see the value of that FormCollection after form submit?
[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);
}
and my last question is that my full code above uses 2 db.Entry().State = EntityState.Modified; but only 1 is working; is there anything that can make the 2nd entity.modified not work? assuming that the checksopen does have a match? I am passing data from the parent model relisting to the child model openhouse , so for every openhouse there is a relisting so I want to capture the changes in both to keep it in sync.
[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);
}
I was wondering is there a way in which I can see the value of that FormCollection after form submit?
It is possible. Your code may pass the parameter into a property of the Model
that you return back after post action. return View(house);
You are returning model named house
. Just add a new public parameter to this model and assign your RegistrationID
to it.
Tip: for performance considerations, there is no need to post the FormCollection
, as it posts every item that you have in your view. Posting only the Model
that contains necessary properties with hidden model values would be a better approach.