Displaying radio buttons using ASP.NET MVC EditorForModel - asp.net-mvc-3

I am using EditorForModel bound to a ViewModel. I have to display a list of radio buttons on my form, but I'm not sure how to do this using Data Annotations. Is this possible? Or can I not use EditorForModel in this case?
My ViewModel can be flexible, as it's only a DTO and I am manually mapping it to my data access layer.

Create a custom editor template:
[UIHint("RenderAsRadioButtonsList")]
public SomeObject ToBeRenderedAsRadioButtons { get; set; }
and then, in Views/Shared/EditorTemplates, create the partial view:
Views
/Shared
/EditorTemplates
RenderAsRadioButtonsList.cshtml

See this Geekswithblogs post on a radio button helper.
Also another one.
A google search will turn up additional resources for radio button lists in ASP.NET MVC.
Here is some code over at GitHub, too.

Related

How to get MVC web grid dropdown column value using javascript or from controller

We have a dropdown list column in ASP.NET MVC 3.0 WebGrid. User will select a item from that list for each row.
When i click on submit I need to capture the selected value for each row.
Can anyone please give me a sample code for doing this from controller.
Thanks in advance
You can make a column with DropDownList control using a html helper:
grid.Column("List", header: "List", format: #<text>#Html.DropDownList("selection", someItems)</text>)
Then put the whole grid in a "form" statement, which refers to "Save" action in your controller, and create that "Save" action method like this:
public ActionResult Save(string[] selection) {
[...]
}
Why don'y you give telerik.mvc grid a try. It's super smooth and handles such scenarios pretty well. Moreover the effort is pretty less in setting it up.
You can download it as a nuget package.
Examples are here http://demos.telerik.com/aspnet-mvc/grid

MVC3 Is It Possible to Create Model from Parent Model in Single View/Controller?

So my MVC ignorance is showing more and more each day. I have a view that is bound to a Model of [Category] which has simply a table of categories and a link that allows you to add a [Item] to that category and am attempting to display that partial view ([InsertCategoryItem]) inside a Telerik.Window object and do an HttpPost or Ajax Post to submit the view model to the controller and have it add the [Item] to the database. Is this possible? If so, can anyone give me a stripped-down example of what the View and controller should look like?
Oh, and my project is MVC3, SqlServerCompactEdition (4.0), Telerik Extensions, Razor views
You can achieve it by using custom toolbar template and grid Editing. By combining these two features you can achieve you required. If you want custom popup then you have to use custom editor template of the type of your model

MVC3: Showing entity relationships in a view

I am new to ASP.NET MVC and I have a question regarding viewing entity relationships.
Say I have an entity called 'Person'. This holds the usual data relating to a person (Name, Email, etc). I also have a 'Notes' entity. Under EF, a 'Person' can have many 'Notes'.
I have a Person controller where I can view and preform CRUD operations on a Person object.
I can show the notes in the view easily but what is the best way to allow a user to add/edit/delete these notes from the Person view? I am hoping to do this using AJAX and not have the user move to a completely different page to add/edit/delete a note.
Thanks in advance,
ViperMAN.
When they edit a note, popup a jQuery dialog pointing to your URL to edit or have a separate Ajax.BeginForm() on the page that the details go into. When they finish the edit call a method to refresh the notes.
So:
1. In your Notes grid (or whatever)
you have an edit link for each note called "edit"
this link looks something like the following:
This one actually uses 'notes' : )
http://www.iwantmymvc.com/dialog-form-with-jqueryui-and-mvc-3
ASP.NET MVC | Problem about showing modal dialog using jQuery dialog widget
ASP.NET MVC modal dialog/popup best practice
Also beware of this scenario for multiple links:
MVC3 - Only first row link works well with Jquery Modal Dialog
Now the urls you use to populate the dialogs would be for example
/Note/Edit/10
One thing to note - jQuery validation needs to know about these new items that are being loaded via ajax into the DOM , so in your partial view you need to tell jQuery validation to include the new items - I'll edit in a bit to add this, have to grab it from another machine.

Sharing viewmodel to multiple views using Caliburn.Micro in WP7

I am currently working on a project which requires multiple views of the same viewmodel. Let me describe this way:
ViewModel: CustomerDetailsViewModel.cs (inherited from Screen class)
View: CustomerDetails.cs (this view has CustomerDetailsViewModel as datacontext and this set automatically by Caliburn.Micro)
View: CustomerInfo.cs (now this is the view where I want to share CustomerDetailsViewModel, which could have some data already modifed via CustomerDetails view)
I am currently using NavigationService to navigate to CustomerInfo view. Is there any way to pass the reference of current viewmodel to the view which user is navigating to in caliburn.micro?
Thanks in advance
idev
Use the attached property cal:Bind.Model="{Binding}" to bind the view to the view model.
See http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions "View First" section.
Alternatively you can also look into the cal:View.Context="MyContext" attached property as described here: http://caliburnmicro.codeplex.com/wikipage?title=Screens%2c%20Conductors%20and%20Composition "Multiple Views over the Same ViewModel" section.
Add a property or two to your App.xaml.cs. What ever you put in here will persist throughout the lifetime of the application (keep in mind that tombstoning will cause this property's value to be lost though). If you want to pass a ViewModel then setyour associated property in App.xaml.cs to the view model and then when the new page loads have it read from that same property.

passing checkbox and textbox values from a view form to a controller action

I have a view with a form..this form has a textbox and a checkbox in it.
i also have a submit button in the form which points to an action in a controller.
my question is..how can i pass the values in the textboxes and the checked state of the checkboxes to the controller action?
the textboxes and checkboxes are not tied to a model.
thanks
You have two options.
The best option would be to create a ViewModel. This model doesn't need to have any logic or anything behind it, just a public get/set for the properties. This means you can also use the MVC validation, helpers and binding on the values and makes it easier to work with.
The alternative is for your action to accept a FormCollection, this is basically a dictionary with the form values, the keys being the name attribute on your form elements.

Resources