Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC4 Passing Complex Object from View to Controller, new View is not rendering
    primarykey
    data
    text
    <p>I'm very new to MVC and have run into this issue while trying to port an existing site to MVC4. </p> <p>We're using models where much of the data is populated by service calls, so obviously we'd like to keep the calls to a minimum. The problem is that when I try to pass the model back to the controller, the complex objects within the model invariably become null. I have been able to persist the data on a call back to the controller using ajax; however, I need the action to return a new view, and after the action completes, the code for the view executes, but there is no redirect (which I believe is the point of ajax, I think what I'm asking for is a solution that will persist the data in the same way but actually redirect).</p> <p>Here is my model:</p> <pre><code>public class DistributionModel { public string typeOfDistribution { get; set; } public Document document { get; set; } public string thumbnailUrl { get; set; } public MergeFieldModel mergeFields { get; set; } } public class MergeFieldModel { public MergeFields documentMergeFields { get; set; } } </code></pre> <p>Here is the controller action I am using:</p> <pre><code> public ActionResult Index(DistributionModel distributionModel) { distributionModel.mergeFields = new MergeFieldModel() { documentMergeFields = MergeFieldsHelper.GetDocumentMergeFields(distributionModel.document.Id) }; return View("Index", distributionModel); } </code></pre> <p>I tried using a href=@Url.Action("Index", Model) instead of the button in the block below to call the controller and perform the redirect (the redirect itself did work, but I then had to perform another service call within the controller to retrieve the same document as I was working with from the calling view) because the Document object within the model kept returning as NULL to the controller.</p> <p>Here is the portion of the view that is calling the controller and actually returns the complete model: I think what I am looking for is a way to accomplish this without ajax so that I can get the redirect to the Distribution/Index page (this is fired from Distribution/DocumentDetails page) </p> <pre><code> &lt;button id="EmailDistribution" data-corners="false" data-theme="a"&gt;EMAIL&lt;/button&gt; $('#EmailDistribution').click(function () { var model = @Html.Raw(Json.Encode(Model)); $.ajax({ url: '@Url.Action("Index", "Distribution")', type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify(model), processData: false, }); }); </code></pre> <p>Thanks, any help would be very much appreciated.</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.
 

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