ASP.NET MVC Scaffolding + associations - asp.net-mvc

I scaffolded a CRUD scenario for my model to start from. Now I need to add a radio button list field (which doesn't exist but the scenario for a dropdownlist is the same so think of that) for one field (an association to another type, think of a foreign key) to my "create"-view with database driven listitems. I found no other way to do this (without losing my strongly typed view) then to wrap my whole model in another one so I can add the list items.
That means I have to adapt almost every line in the view because they are no longer:
#Html.EditorFor(model => model.Name)
but now:
#Html.EditorFor(model => model.Model.Name)
Is there no way around this?

Related

Using #Html.EditorFor to create new model

I want to render a form used to create a new instance of a Model. I tried,
#Html.EditorFor(model => new Person())
But I got the error,
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
I tried,
#Html.EditorForModel("MyNamespace.Person")
but nothing rendered. How do I use Html.EditorFor when I dont have a model instance to pass to it?
It seems that you're a bit confused about how to use #Html.EditorFor(). First,, your use of the lambda expression doesn't quite make sense, model => new Person(). If you want to create a new instance of a model then there's no need to use a lambda, just new Person() gives you that instance.
Second, you don't need an actual instance to pass into #Html.EditorFor(). The purpose of this method is to produce an html markup including an input field for a property or even multiple properties.
So let's say your Person model had an Age property, then you can create an edit field for that attribute by calling
#Html.EditorFor(model => model.Age)
Now where does model come from? You have to define it in your view, so add this line to the top of your view file so that ASP.net knows which model to grab the Age attribute from,
#model <namespace>.Models.Person
The namespace is usually the name of your project, and if you don't put your models in the Models folder, then replace that with where ever your model is contained in.

How to Fill ListBox

I think my question is basic but I can't find the way to do this issue in Asp.Net MVC 3.
I have a Model with the Controller called "Concepto". I want to see into the Create view, a ListBox with all the data from Concepto model.
The Index view show the complete data from the model, so the database connection and EF process are correct.
Hi you should create a strongly typed view using the model. Once you have the model in the context of the view, you can use the MVC helpers to render the data. For example if you have multi line postal address data stored in a property PostalAddress, you can use
#Html.TextAreaFor(m => m. PostalAddress)
You can further customise the text area by providing col and row information as the second parameter
#Html.TextAreaFor(m => m. PostalAddress, new {#cols=”50”, #rows=”20”})
Hope this helps
You can't display all the data in a ListBox. However, you can have a field as List itme's display text and another as its value (like ID) using #Html.ListBoxFor() method which expects a name of a field and a collection of item eg. #Html.ListBoxFor(model => m.someId, Model.CollectionData);
Thanks,

Detect if you are adding or updating within an EditorTemplate View

I have a View that I am using as an EditorTemplate. I want to limit what the user can edit vs. what they can insert. My EditorTemplate View is typed as SomeModel and I know that if SomeModel.Id is not 0, that means we are doing an edit, otherwise we are doing an insert. I thought I'd be able to do something like:
#if (Model.Id == 0)
{
//show "insert-specific" UI
}
but for some reason, I always get 0's, nulls, default, etc. when I check via Model. whereas the Html helper methods pick up the true value just fine, such as:
#Html.TextBoxFor(model => model.Id)
Again, the value of Model.Id is always 0, even when #Html.TextBoxFor(model => model.Id) shows another value.
Is there a better way to achieve what I am trying to do?
Note: Not sure that it matters, but I'm using the Telerik MVC Grid control. It doesn't seem to allow for a different View for inserting vs. editing.
Yo,
If you are using Ajax binding then the EditorTemplate is serialized and send to the client where it is re-populated with different values each time you edit a record or add new record.
If you are using ajax binding then you can use the OnEdit client event and check whether or not the e.mode is edit or insert. Then you can manipulate that editor with JavaScript.

Using multiple partial views in MVC3 razor forms

I have an insurance entry form that has contact information for two people. I have created a Razor partial view for the contact entry and put it in the form twice. The 'master' view model (VmApplicationForm) contains two instances of a subsidiary view model (VmPolicyHolder) corresponding to the two contacts as well as some properties common to both contacts. I am calling #Html.RenderPartial("_CreateOrEdit", Model.contactInfo1) and #Html.RenderPartial("_CreateOrEdit", Model.contactInfo2) in the page. With this arrangement (no surprises) the rendered code has duplicate IDs for the form input elements.
Is there any way of getting RenderPartial to prefix the IDs and Name attributes? I couldn't see this in the documentation, but perhaps I've missed something.
Sorry, I don't have time yet to give you the example code, but i'll give you the idea. You should first create EditorTemplate for that probably called ContactInfo class. And then, in the base class(Holding that two contacts) edit view, you should write
#Html.EditorFor(model => model.contactInfo1)
#Html.EditorFor(model => model.contactInfo2)
This way, it will render that EditorTemplate and generate correct ids and names to inputs within it.
What you are doing is trying to post a collection of items in a form- this can indeed be done in MVC (as well as in any web page/application that uses a FORM tag), however it requires some special handling to avoid id collisions and to correctly format the post data. Steve Sanderson has a great post on how to accomplish this:
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
Essentially its a wrapper to append unique guids to element ids (in your case for each contactInfo), and create the proper array format in the tags. eg <input name="ContactInfo[f2cc4d6b-fc32-45e9-9d3d-fce54c3fede3].FirstName">
if your model is something like ContactInfo, you will end up posting like
[HttpPost]
public ActionResult Index(IEnumerable<ContactInfo> contacts)
{
// To do: do whatever you want with the data
}

Some Confusion / Query Related to MVC2

I have just started using MVC 2 , I have got a couple of queries , may be you guys can help me to clear my confusion.
Why mvc doesnt allows to inherit multiple models on a view page.
For instance if i inherited account model in my view , why i cant excess the associated entity properties with the account model in that view, We are only allowed to use the properties of that model like <%: Model.FirstName :%> Where First name is the property of account model. Why we cannot use <%:Model.account.aspnet_users.vehicle.make %> Where aspnet_users is associated with account through a foreign key vice versa. MVC 2 only allows <%:Model.account.aspnet_users.vehicle %> Therfore I cant use the associated property of vehicle which in this case is vehicle.make. I was thinking of doing something like <%: Html.TextboxFor(model => model.account.aspnet_users.vehicle.make %>.
ASP.NET MVC doesn't restrict navigation to related properties. If you cannot access the property it means either:
The relation is not loaded (it is null) and lazy loading is turned off / context is disposed
The relation is actually a collection an you must use Linq to navigate in the collection

Resources