Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is what I would recommend. Create a div that contains the dynamic content and put that content in a partial view. In this case there would be a partial view called Products.ascx</p> <pre><code> &lt;div id="ProductsContent"&gt; &lt;% Html.RenderPartial("Products"); %&gt; &lt;/div&gt; </code></pre> <p>Call a javascript function when a category checkbox is clicked.</p> <pre><code>&lt;input id="Category_1" type="checkbox" onclick="CategoryCheckChanged(1)" /&gt; &lt;input id="Category_2" type="checkbox" onclick="CategoryCheckChanged(2)" /&gt; &lt;input id="Category_3" type="checkbox" onclick="CategoryCheckChanged(3)" /&gt; </code></pre> <p>use JQuery to detect the value of the checkbox, then post to the server. In the example below my controller name is called Products. The information returned from the server is the updated partial view, which replaces the div contents.</p> <pre><code>function CheckChanged(id) { var bChecked = $("#Category_"+id).attr("checked"); var value = 0; if(bChecked) value=1; $.post('&lt;%= Url.Action("CategoryChanged","Products") %&gt;' , { value: value, categoryid: id } , function(data) { if (data.success) { alert("Sweet !") //update the div with the new content $('#ProductsContent').html(data.newcontent); } else { alert("Bummer:" + data.msg); } }, "json"); } </code></pre> <p>Here is the CategoryChanged function in ProductsController</p> <pre><code> [HttpPost] public ActionResult CategoryChanged(int value, int id) { try { //Save the change to the database SaveChangedCategoryValueToDatabase(id,value); //Create your View Model MyProductsViewModel vm = new MyProductsViewModel(); return Json(new { success = true, newcontent = MyViewHelper.RenderPartialToString(this.ControllerContext, "~/Views/Products/Products.ascx", new ViewDataDictionary(vm), new TempDataDictionary()) }); } catch(SystemException ex) { return Json(new { success = false, msg = ex.Message }); } } </code></pre> <p>and finally... The helper function that takes renders a partial view to a string.</p> <pre><code>public static class MyViewHelper { public static string RenderPartialToString(ControllerContext context , string partialViewName , ViewDataDictionary viewData , TempDataDictionary tempData) { ViewEngineResult result = ViewEngines.Engines.FindPartialView(context, partialViewName); if (result.View != null) { StringBuilder sb = new StringBuilder(); using (StringWriter sw = new StringWriter(sb)) { using (HtmlTextWriter output = new HtmlTextWriter(sw)) { ViewContext viewContext = new ViewContext( context, result.View, viewData, tempData, output); result.View.Render(viewContext, output); } } return sb.ToString(); } return String.Empty; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. 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