I am curious to know the best way to approach the following scenario:
public class LittleThingViewModel
{
public string Id{ get; set; }
public string Name { get; set; }
public string Location { get; set; }
}
public class BigThingViewModel
{
public LittleThingViewModel little1 {get; set; }
public LittleThingViewModel little2 {get; set; }
public string propA {get; set; }
public string propB {get; set; }
public string propC {get; set; }
}
In the initial CREATE action of my controller, I populate two LittleThingViewModels, assign them to a new BigThingViewModel and pass it to the strongly-typed view. I then use EditorFor to render edit controls for BigThing properties A, B, and C.
What is the best technique for automatically rendering the LittleThingViewModels to the form such that when I post back to my controller it auto-binds? (My goal is to only display the Name and keep the other properties hidden)
public ActionResult Create(BigThingViewModel b)
I found that I could render A, B, and C properties in addition to each individual property of the two LittleThing sub-viewmodels and it successfully bind BigThingViewModel during POST:
Html.DisplayFor(model=>model.little1.Name)
Html.HiddenFor(model=>model.little1.Name)
Html.HiddenFor(model=>model.little1.Id)
Html.HiddenFor(model=>model.little1.Location)
Is there a technique that would allow me to render both labels and hidden elements automatically instead of having to do them each explicitly each time?
Something like:
Html.RenderFor(model=>model.little1)
Thanks
Create a partial view that is strongly typed to the LittleThingViewModel.
LittleThingView.ascx (partial view)
Html.DisplayFor(model=>model.Name)
Html.HiddenFor(model=>model.Name)
Html.HiddenFor(model=>model.Id)
Html.HiddenFor(model=>model.Location)
In main View
<%
Html.RenderPartial("LittleThingView",model.little1);
Html.RenderPartial("LittleThingView",model.little2);
%>
To make it look better you can make a collection for these objects in the big ViewModel. So if one of them is null or you need more than one, it wouldn't need any changes.
public class BigThingViewModel
{
public List<LittleThingViewModel> littles=new List<LittleThingViewModel>();
public string propA {get; set; }
public string propB {get; set; }
public string propC {get; set; }
}
And in the view:
<%
foreach(var littleThingViewModel in model.littles)
{
Html.RenderPartial("LittleThingView",littleThingViewModel);
}
%>
Related
I'm learning about the Bind attribute and I have a doubt.
I can use the Bind attribute to include/exclude the data that will be posted, so.
Would it not be better to use a specific ViewModel instead of the Bind attribute?
Think about what happened if your entity changes overtime, then you might force to change all your different viewModels which you have created instead of using Include or Exclude. it will get hard to maintain your code.
Suppose you have this :
public class PersonalViewModel
{
private int PersonalID { get; set; }
public string PersonalName { get; set; }
public string PersonalFamily { get; set; }
public byte? GenderID { get; set; }
public string PersonalPhone { get; set;}
}
Consider these :
public string ShowPersonalToAll(
[Bind(Exclude = "PersonalPhone")]PersonalViewModel newPersonal)
{...}
OR
public class PersonalViewModel
{
private int PersonalID { get; set; }
public string PersonalName { get; set; }
public string PersonalFamily { get; set; }
public byte? GenderID { get; set; }
}
Now What if saving personal's mobile become important! and if you have created different customized ViewModel for several action (depends on application's business)?
Then you have to change the main ViewModel and all the other Customize ViewModel, While by using Exclude there is no need to change ViewModels, no need to change actions and the main ViewModel just changes.
Not sure how to explain this, but here goes...
I've built a code first data model using EF 4.3. One of classes, "Address" contains typical address data, street, city, state, etc. Other classes in the model contain instances of the "Address" class.
The problem. The data will be gathered/presented using different views, some of which will require the address fields, others that will not.
I can build different view models, each having the necessary validation attributes, and copy the data back and forth between data model and view model but that seems wrong.
What am I missing? There has to be a smarter way to do this.
Thanks for your help,
Jimmy
First read these questions and their answers:
MVC: Data Models and View Models
Why Two Classes, View Model and Domain Model?
also this article could help:
ASP.NET MVC View Model Patterns
In conclusion, I think in most scenarios it's helpful to have a chubby domain model (DM) but light weight presentation models (PM) related to it. So when we want to edit only a small chunk of that fat DM, one of our PMs will raise its hand.
Imagine this class in DM:
namespace DomainModels
{
public class Person
{
public int ID { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public DateTime? DoB { get; set; }
public MyAddressDM Address { get; set; }
public string Phone { get; set; }
public IEnumerable<MyCarModel> Cars { get; set; }
//etc.
}
}
Now imagine that in one view we need to edit only Address and Phone. A light weight PM could be like:
namesapce PresentationModels
{
public PersonAddressPhone
{
public int ID { get; set;}
public string FullName { get; set;}
public string AddressSteet { get; set; }
public string AddressCity { get; set; }
public string AddressState { get; set; }
public string AddressZipCode { get; set; }
public string Phone { get; set; }
}
}
and in another view we need to add/remove cars for a person:
namesapce PresentationModels
{
public PersonCars
{
public int ID { get; set;}
public string FullName { get; set;}
public IEnumerable<PMCar> Cars { get; set;}
}
}
Mapping between DO and PM is the golden piece of this puzzle. Be sure to take a look at AutoMapper.
I'm a beginner ASP.NET MVC 3 web-developer and have this problem:
There are several view models, that have similar logic and I've come to idea to have one common EditorTemplate for them to be rendered by Html.EditorFor.
The template is named "ExistOrCreateNewInput.cshtml" and is strongly-typed with IExistOrCreateNewInput interface class:
public interface IExistOrCreateNewInput
{
int? existEntId { get; set; }
IUnapprovedNewEntityCreateInput createInput { get; set; }
}
Template's content is something like:
#model IExistOrCreateNewInput
<h2>Add or choose</h2>
#* here put some common js code *#
Html.EditorFor(o => o.existEntId)
Html.EditorFor(o => o.createInput)
Suppose I have some sort of models that implement this interface, for example:
public class FirstModelInput : IExistOrCreateNewInput
{
[Display(Name="First")]
[UIHint("Lookup")]
public int? existEntId {get; set;}
[UIHint("PaperCreateInput")]
public PaperCreateInput paperCreateInput {get; set;}
public IUnapprovedNewEntityCreateInput createInput
{
get
{
return paperCreateInput;
}
set { }
}
}
public class SecondModelInput : IExistOrCreateNewInput
{
[Display(Name="Second")]
[UIHint("Lookup")]
public int? existEntId {get; set;}
[UIHint("ThesisCreateInput")]
public ThesisCreateInput thesisCreateInput {get; set;}
public IUnapprovedNewEntityCreateInput createInput
{
get
{
return thesisCreateInput;
}
set { }
}
}
public class ThirdModelInput : IExistOrCreateNewInput
{
...
}
Where PaperCreateInput and ThesisCreateInput classes implement IUnapprovedNewEntityCreateInput interface.
So, I want my view model
public class SomeGlobalViewModel
{
[Required]
string name {get; set;}
[UIHint("ExistOrCreateNewInput")]
FirstModelInput firstModel {get; set;}
}
to render correctly the attribute "firstModel" with Html.EditorFor(o => o.firstModel) method.
Now I know that EditorFor method is dealing with Metadata, so probably my question should be like "how can I pass metadata of attributes to base interface attributes". Correct me, if I'm wrong.
Anyway, I need those helper methods
Html.EditorFor(o => o.existEntId)
Html.EditorFor(o => o.createInput)
in my editor template (strongly-typed with interface) to render my Models' attributes as I declared them in implementing classes:
[UIHint("Lookup")]
public int? existEntId {get; set;}
[UIHint("PaperCreateInput")]
public PaperCreateInput createInput {get; set;}
Thanks in advance.
Sorry for my bad English.
I'm having trouble passing view information from my Get/Create action to my view. Here are my three model classes;
public class Competition
{
public int Id { get; set; }
public int CompetitionId { get; set; }
public string Name { get; set; }
public string Prize { get; set; }
}
public class CompetitionEntry
{
public int Id { get; set; }
public int CompetitionEntryId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public int CompetitionId { get; set; }
}
public class CompetitionEntryViewModel
{
public int Id { get; set; }
public Competition Competitions { get; set; }
public int CompetitionId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
Here is my Get/Create action in CompetitionEntry Controller;
public ActionResult Create(int id)
{
CompetitionEntryViewModel competitionentryviewmodel = db.CompetitionEntriesView.Find(id);
return View(competitionentryviewmodel);
}
I know this doesn't work. The id parameter goes into the URL fine. How to I get access to my Competition class in th Get action? I need to be able to show the competion name on my Create Competition entry view.
Thanks in advance!
public ActionResult Create(int id)
{
var data = db.CompetitionEntriesView.Find(id);
CompetitionEntryViewModel competitionentryviewmodel = new CompetitionEntryViewModel();
competitionentryviewmodel.CompetitionName = data.Name;
return View(competitionentryviewmodel);
}
What you are trying to do is build an object graph and display it through a view model. In order to do this, you need to map your domain model(s) to your view model.
You can do the mapping yourself by writing a lot of code (re-inventing the wheel), or, you could consider using third party tools to do this for you. I recommend you use an AutoMapper as it is very simple to use imo.
The other problem is that your view model contains a domain model. This is likely to cause you a lot of headache in near future. If I were you, I would replace Competition with CompetitionViewModel.
I would also consider creating a view model for a list of competitions, i.e. CompetitionsViewModel. Look into partial views to see how you can display a list of competitions.
Good luck
UPDATE #3: Entire question
I have a class HB:
public class HB
{
public int Id { get; set; }
[StringLength(3000)]
public string Text { get; set; }
public Title Title { get; set; }
}
And Title:
public class Title
{
public int Id { get; set; }
public string Name { get; set; }
public int MaxChar { get; set; }
}
Before you can write a HB (which is kind of an article), you have to choose your title, so your StringLength for HB.Text can be determined. Meaning, this article can only have a certain amount of chars, deppending on what 'Title' the writer has. Example: Title1 can only write a 'HB' with 1000 chars, and Title2 can write a 'HB' with 3000 chars. So. Thats means the the StringLength has to come from Title.MaxChar. Whats the smartest way to do that?
The Title entity is prefixed data that will be stored in the db.
To be crystal clear, what I want to achieve is something in the line with: [StringLength(Title.MaxChar)]
Ive done structure/design for this mechanism in Webforms a million times, my brain just cant addapt to mvc, so some help would be appreciated. Code would be even more appreciated.
Pretty sure that is not possible as written. This strikes me as trying to force business logic into the model that belongs in the controller.
In this situation, I would make the attribute on the Text property [StringLength(3000)]. In the controller, during validation, I would write something along these lines:
public ActionResult (HB model)
{
if (model.Text.Length > model.Title.MaxChar){
ModelState.AddModelError("Text", string.Format("Text for this Title cannot exceed {0} characters.", model.Title.MaxChar));
}
if (ModelState.IsValid)
{
//do stuff
return RedirectToAction("Index"); //or something
}
else
{
return View(model);
}
}
I believe this will accomplish what you are trying to do. Now, for the Title object, I'd flatten that out a bit in your model:
public class HB
{
#region Base Properties
public int Id { get; set; }
[StringLength(3000)]
public string Text { get; set; }
#endregion
#region Title Properties
public int TitleId { get; set; }
public string TitleName { get; set; }
public int TitleMaxChar { get; set; }
#endregion
}
This is assuming you need to display that information in your view. If you just need to reference it for your business logic validation, just have the TitleId property and use that to instantiate the Title object in your controller when you need it. Don't forget to make hidden inputs for each of these properties if they are not editable!