I have created a model with Name, Email, Phone no properties and also created views for create, edit, delete and index. All these are working fine.
Now I have added a property in model like Address. Now I want that, is there any method so that, after adding the property in model, the newly added property automatic add in all the respective views.
Can we do this?
There is a way but only applicable if the view is generated by using HTML helper classes. The view should be strongly typed.
#Html.EditorForModel()
This HTML helper will automatically update and genatrate required input fields but with this you might have limited flexibility.
Other way can be creating your own class that renders html page.
Use as below in your view. View should be strongly typed of the model you want to use:
#using(html.BeginForm()){
#Html.EditorForModel()
}
Related
I have an _Address partial view. This partial view contains all of the address fields that match the Address Model. At the top of this view I set the model like this:
#model Data.Address
In my CustomerInfo view I try the following to render the Address fields as part of my form:
#Html.Partial("~/Views/Shared/Partial/_Address.cshtml")
The error I am getting is:
The model item passed into the dictionary is of type 'Data.Customer',
but this dictionary requires a model item of type 'Data.Address'.
I am assuming I am getting this error because the model declared in my CustomerInfo view is of type Data.Customer and it is automatically trying to pass it to my _Address view which has a model of type Data.Address in it.
What is the proper way to make this flow properly? I noticed there is also an #Html.RenderPartial("ViewName", model) helper but have no idea how to pass it Data.Address since the primary model in my CustomerInfo view is Data.Customer.
I guess that your primary model Data.Customer already has one or more properties of type Data.Address that you would like to display. So you would use the #Html.Partial helper and pass the value of this property like that:
#Html.Partial("ViewName", Model.SomeAddressProperty)
As an alternative to using the Html.Partial helper you could use Editor/Display templates. They work by convention. So rename your _Address.cshtml partial to ~/Views/Shared/EditorTemplates/Address.cshtml and then inside your main view use:
#Html.EditorFor(x => x.SomeAddressProperty)
or if the partial doesn't contain input fields or forms you could use a display template: ~/Views/Shared/DisplayTemplates/Address.cshtml and then:
#Html.DisplayFor(x => x.SomeAddressProperty)
The convention here is the name and location of the template. Since the type of the SomeAddressProperty is Address, ASP.NET MVC will look for the corresponding Address.cshtml template.
If your main view model doesn't have an address property you should simply go ahead and add one or more such properties and populate them in the controller action rendering the main view. Then you can call the partial for each of those properties.
First of all, don't pass Data.Customer to your main view, but only 'Data'. This is more flexible, all your have to do then is fix the references inside the main view, so instead of #Model.FirstName, you use #Model.Customer.FirstName. This way, you can call your partial view with a more explicit #Html.Partial("~/Views/Shared/Partial/_Address.cshtml", #Model.Address).
My web application currently has a domain model and a view model. Domain model is almost a representation of my database. So when I insert data into a table, i'm inserting my domain model object. My view model handles how I want to display my ui. So if i have a property on view model that has a drop down list, i give it a custom display attribute drop down list with an enum saying what kind of values to load ([DropDownList(Enums.Product)]. My view model also has display labels and calls specific editor templates for the different properties using UIHint. I feel like creating a view model is becoming repetitive. I'm wondering if there's any harm in just putting display attributes (uihint, display(name="blah")) right on my domain model so i can skip the view model unless specifically need it for something. The only other solution is to actually wite out the html so that i'm using Html.Editor("SomeProperty", Model.SomeProperty) instead of Html.EditorFor(x => x.SomeProperty). Any thoughts?
I have model that has a lot of repeating data structures. But I want to display groups of related data the same way. For example I have a checkbox next to a text box and if that textbox has been recently edited then an arrow is displayed with it. I don't want to write the same code over and over for the same conditions. Is there a way I can call a view structure and pass in parameters like 3 bools and a string and then it would put them in the correct html display template.
I,m using MVC5, with c#
Sure - you should be able to use Display Templates and Editor Templates. First, identify what you can abstract out into a stand alone view model. Based on your question, we can make a stand alone view model with 2 boolean properties and 1 string property.
public class PropertyGroup(){
public bool Checked {get;set;}
public string Text {get;set;}
public bool RecentlyEditied{get;set;}
}
Create a folder in your Shared views folder called EditorTemplates and place a view inside of that folder named PropertyGroup (whatever the name of your ViewModel class is). This would be a partial view and look something like
#model Core.Models.ViewModels.PropertyGroup
<div>
#Html.CheckBoxFor(x=>x.Checked)
#Html.TextBoxFor(x=>x.Text)
#if(Model.RecentlyEdited){
<img src="arrow.gif" />
}
</div>
Now, anyplace that you want that markup to appear, you can do so by called the Html.EditorFor() helper and send that ViewModel class.
#Html.EditorFor(x=>x.PropertyOfTypePropertyGroup)
I want to create a template to format some ViewBags.
Up to now, I had only the case of property of of a class, which I could easily modify with
[UIHint("MYTemplate")]
public virtual float Foo { get; set; }
Every selction of the property will consider "MyTemplate" and formate the output correctly.
How am I able to transfer this 'structure' to a ViewBag, which is defined in a certain controller?
Actuall, you can't. The contents of the ViewBag is Dynamically Resolved at Runtime. As opposed to Model properties. You can only use Templates for properties of a Model.
The only other option would be to add it to your model. The reason behind this is that, Your model (or View Model to be precise) ought to have everything that your view needs to display data. The ViewBag was intended to add some flexibility for data that doesn't necessarily need to be in your model like the contents of a DropDownList. Therefore, if you need to display content from the ViewBag like properties in your page, then you should consider promoting that property to your model.
Alternatively, you could use Partial Views. like so #Html.Partial("_MyTemplate",ViewBag.Data)
I have an enum view model property which I want to hide in the CREATE View but show in the LIST/Index View. The enum is Open,Failed,Succeeded.
For the CREATE View the editor/control should not be visible.
For the LIST View the editor/control should be a selectable combobox
Can this be done somehow with the HiddenInput attribute?
If this can not be done then I want at least to make it visible in both views, but disabled/another control (label instead of combobox).
I am using asp.net mvc 4.0
Actually you should be using different view models. Remember: the first word of view model is view meaning that you define a view model per view. So you will have a CreateViewModel that will be used for the Create view without the enum property in question and a ListViewModel with the enum property that will be used in the Index view.
When doing code reviews I see many developers trying to reuse the same models over different views and ending up with some horrible logic because those models simply are not adapted for those views. They try to hide, they try to write ifs and stuff to perform validation, usually end up in a maintenance nightmare.