How to create dynamic form in MVC - c#

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

Related

Updating Model inside of view

I've been working on a photography page for a while now. Now I've tried to create a page on which user will upload photos to database.
I wanted to give the user an option of selecting photo categories on the create/upload page using checkboxes. I wanted to use a viewmodel with list of categoryview models which would represent the checkboxes.
Now the tricky part that i need some help with is how do I allow user to dynamically add new categories (if he needs to) on the same page. So how would i add new values into the list of categories on view.
Only thing that comes to my mind is to create an ajax post that would call a method that would create the new category in the database. Then i could append a new checkbox representing that category, But then i do not know how i would send that category in the list of categories in viewmodel without refreshing current page. Is this even possible?
If any1 has any ideas how to start on this I would be grateful.

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 implement a Like/Unlike button (or link) with value saved in DB?

I plan to develop a website [asp.net mvc4, C#, simplemembership and EF] where my users can create a post, and users (owner of the post or the others) could like/unlike it. At this point, I can't see how to implement this feature. I thought about many things :
First, my post class will have a boolean Like property, and then, when a user click on the like link(or button) it will save the value in my db. But in that case, I can't know which user like the post.
Second, have post class, and a like class with a postId and a userId inside. On the same principle, when a user click on the like link(or button), it will save the binary value in my db.
I think the second one is the best (maybe there is another better solution), but still, I don't know how to handle the action method of that link or button, which will display, for example "unlike" when a user like the post, and the names of users who liked it.
I don't know if i'm clear... I would like to know if you have any idea on how to do it, or link where you can send me... anything.
I'd implement a controller taking the name of the user who pressed the like button. save the name and id of post in a table in the database and then when you want to find out how many likes the post has just run a select against the database.
select count(*) from likes where linkId = [value];
You should create a table to store like events with at least a user identity and a post id. Then you can create a post view that has a count colon of likes, etc.

c# MVC3 Binding IEnumerable Model to table

I'm working on a university project and trying to learn MVC3 at the same time. I have a shopping basket style page with a table that shows the items. The model contains a list of products purchased.
I can display the products in a table via looping through the Model and displaying, however I need to implement a way to update the quantities. I currently have the quantities displayed in a HTML.TextBox which can be amended, but when I change the value it's not represented as such in the model.
Could somebody please advise how I could do this.
Thanks.
To update the model, you have post the edited values back to the server. To populate the model values in a text box, you can use the Html.EditorFor extension method and Pass in the lambda expression pointing to the quantity property. You can put the shopping cart controls within an HTML form control with an action of "post" and target of the controller route which will update the model. These are very basic operations in Asp.Net MVC, and you should be able to see how it is done in www.asp.net/MVC
Please do some research before posting your questions in the forum.

View for class/subclass objects

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.

Categories

Resources