Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure if I understood your problem exactly but I can tell you that you will need to put every single value of your model in a form posted to a controller action that you want <strong>not</strong> to be null.</p> <p>This is <em>exactly</em> what you do in your ajax call: You currently transform the whole model to json and use the jQuery ability to transform it again to post data. Assuming that you have the following model for example:</p> <pre><code>public class TestModel { public string A { get; set; } public string B { get; set; } } </code></pre> <p>Your javascript code will create a string similar to <code>{ A: 'Value for a', B: 'Value for B' }</code> which will be transformed to a HTTP POST request using jQuery:</p> <pre><code>POST /Controller/Index HTTP/1.1 Host: demo.loc User-Agent: Mozilla/5.0 whatever Content-Type: application/x-www-form-urlencoded; charset=utf-8 A=Value+for+a&amp;B=Value+for+B </code></pre> <p>As a result your <code>Index</code> action will be called and the <code>DefaultModelBinder</code> binds the values to your model properties. This works for primitive types like integers as well as for complex types like collections for example. The <code>DefaultModelBinder</code> handles the transformation of these types.</p> <p>Let's have a look at a more complex model:</p> <pre><code>public class ComplexSubModel { public ICollection&lt;string&gt; StringList { get; set; } } public class ComplexModel { public ComplexSubModel SubModel { get; set; } } </code></pre> <p>The <code>DefaultModelBinder</code> is also able to bind models like those:</p> <pre><code>POST /Controller/Index HTTP/1.1 Host: demo.loc User-Agent: Mozilla/5.0 whatever Content-Type: application/x-www-form-urlencoded; charset=utf-8 ComplexModel.SubModel.StringList[0]=First+entry&amp;ComplexModel.SubModel.StringList[1]=Second+entry&amp;ComplexModel.SubModel.StringList[2]=Third+entry </code></pre> <p>This will result in a new instance of <code>ComplexModel</code> with its <code>SubModel</code> property set to a new instance of <code>ComplexSubModel</code> with its property <code>StringList</code> set to a new instance of <code>System.Collection.Generic.List&lt;string&gt;</code> containing three strings <code>First entry</code>, <code>Second entry</code> and <code>Third entry</code>.</p> <p>Now what you have to do is render your model properties to hidden fields for example so that they are included in a postback:</p> <pre><code>@using (Html.BeginForm()) { @Html.HiddenFor(m =&gt; m.SubModel.StringList[0]) @Html.HiddenFor(m =&gt; m.SubModel.StringList[1]) @Html.HiddenFor(m =&gt; m.SubModel.StringList[2]) } </code></pre> <p>Every property included in the postback will then not be null <strong>but</strong> could have been forged by the user because they are simple re-transmitted to the server <em>assuming</em> that they were rendered in hidden fields. In fact you cannot be sure that the re-transmitted values are those you fetched by a service call previously.</p> <p>Another possibility would be to save the results of a service call in the <code>TempData</code>-dictionary which in fact stores the values in a user-session and destroys them as soon as they are re-read in the postback action or else directly store the values in a session:</p> <pre><code>public ActionResult Index() { // Do service calls #region Variant a TempData["ServiceResultA"] = foo; TempData["ServiceResultB"] = bar; #endregion #region Variant b Session["ServiceResultA"] = foo; Session["ServiceResultB"] = bar; #endregion var model = new DistributionModel(); // Set properties and stuff return View("Index", model); } [HttpPost] public ActionResult Index(DistributionModel model) { // Read "cached" service calls #region Variant a var foo = (TResultA)TempData["ServiceResultA"]; var bar = (TResultB)TempData["ServiceResultB"]; #endregion #region Variant b var foo = (TResultA)Session["ServiceResultA"]; var bar = (TResultB)Session["ServiceResultB"]; #endregion // Do stuff return RedirectToAction(...); } </code></pre> <p>Both of the variants have pros and contras, like they can be problematic when browsing in two tabs within one browser session for example or the need for the classes to be serializable when you are using a session state server. Nevertheless the procedure is always the same: You will either have to </p> <ul> <li>fetch the data everytime you need it from the services (what is expensive) or</li> <li>save them anywhere on the server (TempData, Session and stuff) or else</li> <li>submitting them with the form (can be forged by the user, is not always easy).</li> </ul> <p>Choose your poison. ;-)</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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