I use MVC 4, and usually Visual Studio will create all kinds for you. I have one form that has only one field, and I just want to insert the create form into the index view.
So the index has something like @model IEnumerable<Models.LinkModel>
This way I access it by iterating through the Model collection.
But if I try to insert a form for the create action, I need @model Models.LinkModel and Model.Name also accesses it. Is there a way to do this or use a different variable name?
Ok, here is some additional information.
SO I have a model.
public class LinkModel { public string LinkUrl {get;set;} }
I have a controller that has Create and Index ActionResults.
Now in the "Index" view, I have
@model IEnumerable<Models.LinkModel> @{ ViewBag.Title = "Links"; }
I can do all my fantastic logic to list all the links.
@foreach(link in Model) { <p>link.LinkUrl<p> }
The Create View has
@model Models.LinkModel // Note that it is just one item not IEnumerable @{ ViewBag.Title = "Add Link"; } @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset class="editor-fieldset"> <legend>LinkModel</legend> <div class="editor-label"> @Html.LabelFor(model => model.LinkUrl) </div> <div class="editor-field"> @Html.TextBoxFor(model => model.LinkUrl) </div> <p> <input type="submit" value="Add Link" /> </p> </fieldset> }
Now it seems pretty silly to create a form for just one field. I want to place this form on the index page. The problem is that I access the object using the Model variable. I wanted to know if there is a way to have two separate instances or to have access to model objects with different names.
Danny source share