public class Job
{
public int jobID { get; set; }
public string description { get; set; }
public string companyName { get; set; }
public JobType jobType { get; set; }
}
public class JobType
{
public int jobTypeID { get; set; }
public string typeName { get; set; }
public List<Job> jobList { get; set; }
}
Now I want to write a ActionResult AddJob-Method, which created an job instance puts it in the jobs DB and adds it to the List<Job> of a JobType:
My first Q ist: How do I pass the jobtype instance from the view to the post-AddJob ActionResult?
My 2nd Q : How Do I set the jobtype field of the new job in the view (as it must be the one which is save in the ViewBag)? Or do I have to set it in the post-ActionResult?
It will then get posted along with your form data when you post the form. The posted job type id will be bound to the second argument by the ModelBinder.
SnafuBernd
Member
2 Points
11 Posts
[MVC 4 WebApp] Passing id from view to ActionResult method.
Jan 28, 2013 08:55 PM|LINK
Hey,
I have the following data model :
public class Job { public int jobID { get; set; } public string description { get; set; } public string companyName { get; set; } public JobType jobType { get; set; } } public class JobType { public int jobTypeID { get; set; } public string typeName { get; set; } public List<Job> jobList { get; set; } }Now I want to write a ActionResult AddJob-Method, which created an job instance puts it in the jobs DB and adds it to the List<Job> of a JobType:
The ActionResult methods:
// // GET: /ManageJobs/CreateJobType public ActionResult AddJob(JobType jobtype) { ViewBag.JobTypeID = jobtype; return View(); } // // POST: /ManageJobs/Create [HttpPost] public ActionResult AddJob(Job job, JobType jobtype) { if (ModelState.IsValid) { jobtype.jobList.Add(job); db.Jobs.Add(job); db.JobTypes.Add(jobtype); db.SaveChanges(); return RedirectToAction("Index"); } return View(job); }The AddJob View:
@model GeliexMVC4App.Models.Job @{ ViewBag.Title = "AddJob"; } <h2>AddJob</h2> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>JobType</legend> <div class="editor-label"> @Html.LabelFor(model => model.description) </div> <div class="editor-field"> @Html.EditorFor(model => model.description) @Html.ValidationMessageFor(model => model.description) </div> <div class="editor-label"> @Html.LabelFor(model => model.companyName) </div> <div class="editor-field"> @Html.EditorFor(model => model.companyName) @Html.ValidationMessageFor(model => model.companyName) </div> <p> <input type="submit" value="Create Job Typ" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }My first Q ist: How do I pass the jobtype instance from the view to the post-AddJob ActionResult?
My 2nd Q : How Do I set the jobtype field of the new job in the view (as it must be the one which is save in the ViewBag)? Or do I have to set it in the post-ActionResult?
innological
Member
142 Points
24 Posts
Re: [MVC 4 WebApp] Passing id from view to ActionResult method.
Jan 29, 2013 02:58 AM|LINK
In your view, withing your html form, jjust include a hidden field like below
<input type="hidden" name="jobtype" value="@ViewBag.JobTypeID" />
It will then get posted along with your form data when you post the form. The posted job type id will be bound to the second argument by the ModelBinder.
DotCastle Team
http://www.dotcastle.com
Note: Please mark this post as ANSWER if it addresses your question/issue