I have a list of object as model. I have properties like Server, Database, User.
What i want is a dropdown with list of object bound to it and on the change of dropdown items, I have to populate the Database and User information in 2 different labels.
How can i achieve that using Model Binding ??
I can't understand what do you mean when asking about 'using Model Binding'. But it can be done with jquery/javascript. You create dropdownlist on page and on change event send selected item to server and receive 'Database and User information'. Example using Ajax helper: using ajax with dropdownlist mvc3
Related
I have a form that only has a checkbox. If the checkbox is not checked then the page won't be valid and won't submit the data. On the view I have out putted some fields from the model object, just to let the user know what they are deleting. When I click the Submit button and the model state is not valid the page will show the validation error but the data from the model is no longer there!
Does anyone know how I can persist this data without making another call to the database to populate the model object?
Thanks
ASP.Net MVC will not magically persist model info.
You need to either include it in hidden fields in the form (which an attacker can modify), or re-fetch it from the database.
If you don't repopulate the Model from the DB, you have to postback all the data, using hidden fields for example.
Anyway, I don't understand how the model validation could fail, being just a checkbox :/.
I have a Model object with a List collection of sub objects. I need to load the form fields for the sub object to the page, and provide a link to dynamically add form fields for a second object. So in other words, multiple sub objects can be created via one form and one post back.
My first thought was just to put the form fields in a partial view and load the view via Ajax.ActionLink. This worked but the problem comes in when trying to uniquely identify each object in the collection and bind the collection of objects on postback. For this it seems the correct usage would be to use an #Html.EditorFor() helper, but I don't know how to call that via Ajax to dynamically add the object's editor template to the page when the link is clicked.
It sounds like you were on the right track with regards to a partial view, you'll just need to sort out the name property of your inputs so you can correctly bind to a collection of objects.
Have a look at this article by Phil Haack explaining how binding to lists works, or google for something more up to date with razor syntax.
Can we create a WEB GRID dynamically using JAVASCRIPT ?
I am using MVC 3 and Razor as my view.
On one of my Razor view I have a drop down box what I want is to create a web grid dynamically on selection of a drop down I get the data depending on the value selected from the drop down box.
Please help me on this.
I had to use ajax for this, where I had called a method using ajax and my method (present in the controller) returned a partial view.
My partial view had the grid I needed to show. and in the method (present in the controller) I had passed value from the dropdown box using ajax, and using this value I had queried my database and sent that model to the partial view.
I'm trying to use a html.dropDownList helper with a strongly typed view model with ajax. I can't the post the code because of the nature of the project.
Here basically what I'm doing...
loading a mvc view via a strongly type view model
clicking a button which does an ajax post to a controller method
using the TryUpdateModel to parse the view model
processing the request
rendering a parital view for the ajax request
According to the article listed below, the problem is that "ASP.NET MVC assumes that if you’re rendering a View in response to an HTTP POST, and you’re using the Html Helpers, then you are most likely to be 'redisplaying a form' that has failed validation."
http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx
Instead of "redisplaying the same form value", I need the html.dropDownList to be set to the same value in the view model.
Does anyone know of any custom dropDownList helpers or have any ideas of how to achieve this?
Things I've already tried/considered
per the blog, manually removing the modelstate item...didn't work - didn't pick up the value in the view model - just defaulted to the first item in dropdown list
considered just writing a regular select list...but this is sloppy and cumbersome since I'm rending multiple select lists in a loop
writing my own custom dropDownList helper...wanted to avoid reinventing the wheel
Thanks in advance
Its not fully clear to me what your problem exactly is, but I've had a similar problem. I used the Html.DropDownListFor(, SelectListItem[]) helper. At postback it sets the value to the choosen one. Your postback view doesn't require to have all the fields of the original model.
#Html.DropDownListFor(model => model.SelectedValue, MyModels.DropDownSelectables());
Here I want the selected value as the model.SelectedValue variable and within my (seperate) model I made a array of selectlistitems. The rest is automagic.
Hope it helps, D
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