I have a an object view with data and a button on that view. The user can review the object info and click the button to go to a new view form whee he can enter info to create an item. My challenge is this, how would I attach the ID of the object on the previous view to associate it with and attach to the information they create and submit?
-
can you show some code ? it will be helpful to solve the problem – Ravi Gadag Jul 02 '12 at 04:27
-
I'm sending from my iPad right now so notat the moment. Do u know of a good solution to fit this conceptually? – J0NNY ZER0 Jul 02 '12 at 04:31
2 Answers
@Html.ActionLink("Add","AddNotes","Object",new {@id=5},null)
This will create an a tag with querystring ?id=5
. (You may replace the hardcoded 5 with the dynamic value in your view)
Have a property to keep this value for your ViewModel/Model
for the create form.
public class CreateNoteViewModel
{
public int ParentId { set;get;}
public string Note { set;get;}
//Other properties also
}
Read this in your GET action
method which creates the second view and set the value of that property of the ViewModel/Model.
public ActionResult AddNotes(int id)
{
var model=new CreateNoteViewModel();
model.ParentId=id;
return View(model);
}
And in your strongly typed view, Keep this value inside a hidden variable.
@model CreateNoteViewModel
@using(Html.BeginForm())
{
@Html.TextBoxFor(Model.Note)
@Html.HiddenFor(Model.ParentId)
<input type="submit" />
}
Now in your HttpPost
action , you can get the Object Id from your POSTED model's ParentId property
[HttpPost]
public ActionResult AddNotes(CreateNoteViewModel model)
{
if(ModelState.IsValid()
{
//check for model.ParentId here
// Save and redirect
}
return View(model);
}

- 214,206
- 104
- 411
- 497
-
I am working on this solution right now. I will thank you by marking as answer if I succeed. Thanks for the code. ( : – J0NNY ZER0 Jul 02 '12 at 18:57
-
-
-
-
1Use ViewModels wherever possible over ViewData and Session. This keeps your code strongly typed and clean. ViewData is dynamic and using that so much produces unmanagable ugly code. – Shyju Jul 02 '12 at 20:12
You could use hidden input & viewdata, PSEUDOCODE. NOTE you may have to use strings with view data and convert back to your id in your controller. See this link for a basic explanation of ViewData/ViewBag (and cons).
You'll need to pass the data to the view from the first action (Controller) The Controller base class has a "ViewData" dictionary property that can be used to populate data that you want to pass to a View. You add objects into the ViewData dictionary using a key/value pattern.
controller
public ActionResult yourfirstaction()
{
//assign and pass the key/value to the view using viewdata
ViewData["somethingid"] = ActualPropertyId;
in view - get the value use it with the hidden input to pass back to next controller to render next view
<input type="hidden" name="somethingid" value='@ViewData["somethingid"]' id="somethingid" />
controller
public ActionResult yournextaction(string somethingid)
{
//use the id
int ActualPropertyId = Convert.ToInt32(somethingid);

- 1
- 1

- 1,570
- 2
- 15
- 17
-
Where would I place the hidden input? I assume the page where the user is coming from... – J0NNY ZER0 Jul 02 '12 at 17:39
-
where should the ACTUAL name of my property that I want to pass go? Can you edit your pseudocode and replace with "ActualPropertyId"? – J0NNY ZER0 Jul 02 '12 at 18:12