Follow-on from this question: If I want to calculate a value based on a user's selection from a drop down list, and put that value into a form variable/model property, how do I do that?
相关问题
- Entity Framework throws exception - Network Relate
- Slow loading first page - ASP.NET MVC
- TextBoxFor decimal
- How to do an inline style with asp.net mvc 3 razor
- How to access the System.ComponentModel.DataAnnota
相关文章
- “Dynamic operations can only be performed in homog
- Change color of bars depending on value in Highcha
- How to get server path of physical path ?
- Breakpoint in ASP.NET MVC Razor view will not be h
- How to define function that returns html in asp.ne
- How to find the exceptions / errors when TryUpdate
- ASP.Net MVC 3: optgroup support in Html.DropDownLi
- A circular reference was detected while serializin
I guess (info from the previous question) you want to get the Item Price data using ajax based on the selected Item in the dropdown and send that to your action method as part of the normal form post.
Step1) Create the ViewModel for Product.
Step2) Create a Product Controller like this
Step 3) Add the Strongly typed View
When you change the selected item in the drop down, using jQuery ajax, it will make a call to GetPrice Action method and get the data. Show it in the div and set as the Value of HiddenField for ItemPrice.
and When you post the Form, You will have it present in the ViewModel posted.
Hope this helps.
Really, if I had a single advice to give any ASP.NET MVC developer that would be: use a view model and forget about ViewBag/ViewData. In 99% of the cases that is the solution to his questions/problems.
So here's the minimal view model that will allow you to properly represent a dropdown list:
then have a controller action that will populate and pass this view model to the view:
then you could have a corresponding strongly typed view to this view model that could contain a form and the dropdown list:
and finally you could have a corresponding action on your controller to which this form will be submitted and inside which you will be able to retrieve the selected value from the dropdown list: