Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This requires quite a bit of tinkering with ajax. Hope you get the idea.</p> <p>I'd just populate the dropdownlist with SelectListItems where Value is the primary key of Plan_S like you would normally do. This allows scalability to the code incase your Plan_S expands and you'll need more edit fields:</p> <pre><code>model.Plans = db.Plan_S .Select(p =&gt; new SelectListItem { Value = p.PlanId_PK, Text = p.PlanNames }) .ToList(); </code></pre> <p>In html, add a hidden field for the id so we know what plan we are editing at the moment. We also need to save the plancost so it won't be null on update. Note that I also use class to hide plan fields so we have correct markup when hiding multiple rows:</p> <p></p> <pre><code>&lt;tr id="ddl"&gt; &lt;td&gt;Select plan&lt;/td&gt; &lt;td&gt;@Html.DropDownList("PlanNames", Model.Plans, "--select--") &lt;/td&gt; &lt;/tr&gt; &lt;tr class="editplanfield"&gt; &lt;td&gt;@Html.Label("Edit Plan : ")&lt;/td&gt; &lt;td&gt;&lt;input id="plannames" type="text" /&gt; &lt;input type="hidden" id="planid"/&gt; &lt;input type="hidden" id="plancost"/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr class="editplanfield"&gt; &lt;td&gt;@Html.Label("Hours :")&lt;/td&gt; &lt;td&gt;&lt;input id="planHours" type="text" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;@Html.Label("Edit :")&lt;/td&gt; &lt;td&gt;&lt;input id="editbutton" type="button" value="Edit" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;@Html.Label("Save Changes :")&lt;/td&gt; &lt;td&gt;&lt;input id="savebutton" type="submit" value="Save" /&gt;&lt;/td&gt; &lt;/tr&gt; </code></pre> <p></p> <p>Then on editbutton click, we request the selected plan from server and fill the editfields:</p> <pre><code>$("#editbutton").click(function () { $("#ddl").hide(); $(".editplanfield").show(); // Request plan of selected id from server $.getJSON('@Url.Action("GetPlan")', { planId: $("[name='PlanNames']").val() }, function (plan) { // Fill the fields according to the Jsondata we requested $("#planid").val(plan["PlanId_PK"]); $("#plannames").val(plan["PlanNames"]); $("#planHours").val(plan["Hours"]); $("#plancost").val(plan["PlanCost"]); }); }); </code></pre> <p>Here is the server side controller code for the request:</p> <pre><code> public JsonResult GetPlan(int planId) { var plan = db.Plan_S.Find(planId); return Json(plan, JsonRequestBehavior.AllowGet); } </code></pre> <p>Now we need to save changes without postback so we need more ajax:</p> <pre><code>$("#savebutton").click(function () { // Parse the plan from fields var plan = new Object(); plan.PlanId_PK = $("#planid").val(); plan.PlanNames = $("#plannames").val(); plan.Hours = $("#planHours").val(); plan.PlanCost = $("#plancost").val() // Post it to server $.ajax({ type: "POST", url: "@Url.Action("UpdatePlan")", success: function (result) { //Postback success $(".editplanfield").hide(); $("#ddl").show(); }, data: plan, accept: 'application/json' }); }); </code></pre> <p>Then on serverside we need the action to update the plan:</p> <pre><code> [HttpPost] public JsonResult UpdatePlan(Plan_S plan) { /* Update the plan in database */ /* Just return something for now */ return Json("Success"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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