Note that there are some explanatory texts on larger screens.

plurals
  1. POData from view to different controller
    text
    copied!<p>2 Points 3 Posts Data from view to different controller 1 minute ago|LINK</p> <p>Hello friends</p> <p>I'm from Colombia, sorry for the writing</p> <p>I have a problem I do not know how to connect two controllers from one view, I'm using EF Data Base First, this automatic genre classes to my model, one of these classes is called "UNIVERSITY" which is a table of the database where are all the universities, and another class is "CAMPUS_UNIVERSITY" where are all the campus of a university, a university can have multiple campus </p> <p>I have 2 controllers and their associated views</p> <p>The first controller is "University" (I think i not necessary show) and your view "Index" (list all Universidadades). That is this:</p> <pre><code>@model IEnumerable&lt;RolesMVC3.Models.UNIVERSITY&gt; @{ ViewBag.Title = "Index"; } &lt;h2&gt;Index&lt;/h2&gt; &lt;p&gt; @Html.ActionLink("Create New", "Create") &lt;/p&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt; Nombre &lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; @foreach (var item in Model) { &lt;tr&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Name) &lt;/td&gt; &lt;td&gt; @Html.ActionLink("Add Campus", "Create", "CampusUniversity" new { id=item.IdUniversity }) | @Html.ActionLink("Edit", "Edit", new { id=item.IdUniversity }) | @Html.ActionLink("Delete", "Delete", new { id=item.IdUniversity }) &lt;/td&gt; &lt;/tr&gt; } &lt;/table&gt; </code></pre> <p>The link "Add Campus" routed to controller "CampusUniversity" to the action "Create". That is this:</p> <pre><code>public ActionResult Create() { ViewBag.IdCityCampus = new SelectList(db.CITY_CAMPUS, "IdCityCampus", "Name"); ViewBag.IdUniversity = new SelectList(db.UNIVERSITY, "IdUniversity", "Name"); return View(); } // // [HttpPost] public ActionResult Create(CAMPUS_UNIVERSITY campus_university) { if (ModelState.IsValid) { db.CAMPUS_UNIVERSITY.AddObject(campus_university); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.IdCityCampus = new SelectList(db.CIUDAD_SEDE, "IdCityCampus", "Nombre", campus_university.IdCityCampus); ViewBag.IdUniversity = new SelectList(db.UNIVERSIDAD, "IdUniversity", "Nombre", campus_university.IdUniversity); return View(campus_university); } </code></pre> <p>And the associated view (View "Create" Controller " CampusUniversity ") is:</p> <pre><code>@model RolesMVC3.Models.CAMPUS_UNIVERSITY @{ ViewBag.Title = "Create"; } &lt;h2&gt;Create&lt;/h2&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;SEDE_UNIVERSIDAD&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.IdUniversity, "UNIVERSIDAD") &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownList("IdUniversity", String.Empty) @Html.ValidationMessageFor(model =&gt; model.IdUniversity) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.IdCityCampus, "CITY_CAMPUS") &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownList("IdCityCampus", String.Empty) @Html.ValidationMessageFor(model =&gt; model.IdCityCampus) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.AddressCampus) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.AddressCampuse) @Html.ValidationMessageFor(model =&gt; model.AddressCampus) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.PhoneCampus) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.PhoneCampus) @Html.ValidationMessageFor(model =&gt; model.PhoneCampus) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.MailCampus) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.MailCampus) @Html.ValidationMessageFor(model =&gt; model.MailCampus) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } &lt;div&gt; @Html.ActionLink("Back to List", "Index") &lt;/div&gt; </code></pre> <p>I want to do that by clicking "Add Campus" appears the name of the university comes fromthe table or class "UNIVERSITY" in the view Create (controller "SedeUniversidad") and that can save the campus of the university and not as I have now, because I have to choose the university of a DropDownList, how I can do?</p> <p>Thank you, blessings!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload