Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I understood correctly you have a single view where you want to display list of assets in one partial view and another partial view to edit the asset when selected.</p> <p>First you should create the two partial views.</p> <p><strong>Assets.cshtml (partial view)</strong></p> <pre><code>@model IList&lt;Models.Asset&gt; .. iterate the model and display the menu of assets </code></pre> <p><strong>EditAsset.cshtml (partial view)</strong></p> <pre><code>@model Models.Asset .. create the form and render the fields for editing </code></pre> <p>Now in the main view <code>Index.cshtml</code> you have to call the <code>Assets.cshtml</code> using <code>Html.Partial/Html.RenderPartial</code> while the other one will be called when click of asset link.</p> <p><strong>Index.cshtml</strong></p> <pre><code>@Html.Partial("Assets", Model.Assets) @*rendering the partial view*@ ... other html &lt;div id="editAssetContainer"&gt; @*edit form placeholder*@ &lt;/div&gt; </code></pre> <p>Note that you should also have a placeholder called <code>editAssetContainer</code> where you are going to display the edit form.</p> <p>Now the pending thing is how you can render the edit form in the placeholder on click of asset links. You can do that through two ways: either directly using jquery or using <a href="http://code-inside.de/blog-in/2011/04/26/howto-ajax-actionlink-asp-net-mvc-3/">Ajax.ActionLink</a>. You can create all the asset links in the <code>Asset.cshtml</code> partial view as ajax links. )If you are using Ajax.ActionLink don't forget to include the unobtrusive ajax library)</p> <p><strong>Ex. of Ajax.ActionLink</strong></p> <pre><code>@Ajax.ActionLink(linkText, "EditAsset", // the action that returns the partial view new {assetId = @asset.Id }, // the assetId that to be passed to the action new AjaxOptions // you can specify the targetid and others here.. { UpdateTargetId = "editAssetContainer", InsertionMode = InsertionMode.Replace } ) </code></pre> <p>In both cases you need an action in the controller that returns the <code>EditAsset.cshtml</code>.</p> <pre><code>public PartialViewResult EditAsset(int assetId) { var asset = .. get asset using assetId from db. return PartialView(asset); } </code></pre> <p><strong>UPDATE:</strong></p> <p>There are two ways you can load the model into <code>Assets.cshtml</code> partial view. The first approach is you can create a view model that contains the list of assets as a property and you strongly type the <code>index</code> view with this view model. Then you can call the <code>Html.Partial</code> passing the assets to it.</p> <p>Ex.</p> <pre><code>public class IndexViewModel { public IList&lt;Asset&gt; Assets; .. other properties if there any } </code></pre> <p><strong>Index.cshtml</strong></p> <pre><code>@model IndexViewModel @Html.Partial("Assets", Model.Assets). </code></pre> <p>The second aproach is you can have a child action that gets the list of assets from database and return the partial view. In this case you don't need to go for strongly typing the <code>Index</code> view.</p> <pre><code>[ChildActionOnly] public PartialViewResult Assets() { var assets = .. get from db return View(assets); } </code></pre> <p>You can call the child action from the <code>Index</code> as</p> <pre><code>@Html.Action("Assets") </code></pre> <p>You can use which one best suits for you.</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