View for class/subclass objects - c#

Step 2 in my quest for today is creating ASP.NET MVC 3 with Razor views that are able to handle class-subclass objects. So i want a form/page that can handle Person -> Woman or Person -> Man based on the selection of a DropDownList (Man/Woman).
The stuff is stored in the database using EF 4.1 Code First. Which works fine. Cause when I store a Woman, and I retrieve with context.Person.Where(p => p.Id == 1) it gets me a Woman object.
The problem I have is that I don't know what to use as a Model in the view.Person would be the most obvious, but then how would I be able to show/access/store the properties for Woman/Man. And in case of a create, the type is not known before hand (server side). The user can select either a Woman/Man, and expects to fill out the specific properties of a Woman or Man.
See this question for the model.

One way would be to create a separate partial view for Man and Woman, then load both views into divs on your page. Then use jquery to Hide/Show the divs after the drop down list selection changes.

Related

Knockout partial view dynamic lazy loading

I am currently stuck with a scenario that I need to achieve and I am hoping you would be able to help me.
The scenario I am facing is as follow:
I am displaying a list of objects with a basic summary of information. These objects are populated from c# code via a web service call that retrieves the data. For each one of these objects that are then displayed there is a select button. When this select button is clicked, another div right below the current summary div should load a partial view. This partial view must also retrieve more data via web service and display the detailed view for the current object that was selected.
I have read the article on using the ko.onDemandObservable (http://www.knockmeout.net/2011/06/lazy-loading-observable-in-knockoutjs.html) which seems to be doing what I want, but I am unsure on how to re use the same template as detail view for each summary object.

Putting ui attributes on domain model

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?

In ASP.NET MVC4 - I need to edit a record and add many (1 to many) sub-records to it (while in Edit mode on the top level record)

Here's my list of data:
Going into Edit mode on the first record:
Here on this Edit page, I need to add multiple "Departments" to this particular contact record. I have a 1 to many relationship (1 contact record to many department records).
Ideally, I'd like to have something like an "Add New Department" button, where the user clicks on it and it will create form fields that tie to department. User fills them out, clicks Save, and has the option to again add another department by way of "Add New Department".
I'm not exactly sure where to start - how can I go about accomplishing this?
That job can't be achieved only by mvc stuff. To achieve your goal, you need to do some extra works by jquery, ajax and partial views.
The main idea is to have a partial view to representing a detail row (when in create phase) and another partial view for getting all details of a master entity (when in edit phase).
A brief example is here. A full howto is here.

How to create dynamic form in MVC

This is a very simple thing to do with controls in asp.net, but I have no idea how to accomplish it in MVC. I have a page where I want the user to type in his employee id. If the employeeid is in the database, I want to remove the log in and show a survey for them to fill out. If the employeeid is not in the database, I want to show them a form to collect their employee information. After they submit their information, I want to show them the survey.
They can fill out several surveys, so I would like to have the survey submit to the same page with the option of creating a new survey or editing one they have previously done. Any time they come to this page and type in their employee id, I want to show them a list of their previous surveys with the option to create a new one.
How can I accomplish this in MVC? Do I create a view and use partials for the survey and log in form? I'm not sure how MVC best handles this sort of scenario.
You can change what is shown in the view easily based on the supplied ViewMdel (or ViewBag if you use that). For example something like:
#if (Model.HasEmployeeID)
{
<form>
<!-- your form here -->
</form>
}
else
{
<div class="survey">
<!-- your survey here -->
</div>
}
For something that you are only going to use once, you can just leave it all in one view. For something that will be reusable (or if you just like that level of organization) you could make the form and/or survey a partial view.
Based on your description I would expect the survey at least to be well suited for a partial view.
I would recomend that you research a little bit on the MVC pattern for web applications
and start here...
http://www.asp.net/mvc
For the login scenario check these post out
http://msdn.microsoft.com/en-us/library/ff398049(v=vs.100).aspx
http://www.codeproject.com/Articles/578374/AplusBeginner-27splusTutorialplusonplusCustomplusF
I would general have a structure like this
http://myapp.com/surveys/ Actions-> (all, single create, single update, single delete)
http://myapp.com/trainings Actions ->(all, single create, single update, single delete)
http://myapp.com/users/ Actions-> (create, update, delete)
Surveys are the surveys with the actions... Trainings are the filled out surveys aligned to the users... and users are the users...
This could be a simple run down of how to structure the mvc routs for the first shot... devil is in the details =)... like always ...
But this should give u enough food to start...
HTH

Using Commands declared in "Parent" ViewModel (MVVM)

(Note: I chose to not use the Navigation Framework)
I have a WizardViewModel which is linked to WizardView.
The WizardViewModel declares and instantiates a command "Next".
It also contains a Property "ActiveSpell" of Type SpellViewModel.
The SpellViewModel contains several PageViewModels, each having a View counterpart.
The ActivePage Property (on SpellViewModel) tells the ui which view to take.
Now I have the following problem:
When I click a button to switch to the next page,
I need access to the "Next" command defined in the WizardViewModel,
but I only have access to a PageViewModel there.
I could just add a Parent property to each child ViewModel,
but I'm not sure if that is a good idea.
Or maybe there is another nicer/common way to do that.
You can use Event Aggregator, to adjust the interaction between ViewModels.
You don't need Parent property. Your view model structure is good, just look at the picture, to understand how you should bind your view model onto the view:
Next command should be implemented something like that:
public void NextExecute()
{
ActualSpell.MoveToNextPage();
}
UPDATE:
According to your comment, Arokh, I've updated the post.
I think, in this case you should implement ActivateCreatePersonSpell command in WizardViewModel.This command should:
save actual spell state
open CreatePerson spell
once person is created set saved spell with result of creation person
The last what you need to do is to bind ActivateCreatePersonSpell command to button on the page. I propose to use ViewModelLocator for these purposes.Look at this post for example.
I had to implement a wizard once and I liked and mimicked the way Josh Smith and Karl Shifflett set up their WizardViewModel and wizard page view models in this example project (source code available with the article):
http://www.codeproject.com/KB/WPF/InternationalizedWizard.aspx
They kept the Next command as part of their WizardViewModel, but created a WizardPageViewModelBase that all of the wizard pages derive from. That allowed the WizardViewModel to control which page is the current page, and it allowed the WizardViewModel to query the current page view model to see if the Next command can execute, thus enabling or disabling the Next button on the wizard. (That is, the wizard view model knew about the page view models, but the page view models didn't need to know anything about the "parent" wizard view model.)
As for adding links to parent view models, it's an approach that works, and I've done it before when I started working with MVVM, but after time I found the approach to result in some difficult to maintain code as every view model becomes interdependent.

Categories

Resources