c# - Asp.Net MVC - Binding of parameter to model value! -


It seems that the model binding is causing problems for me.

Originally I have a product called product operation. For the purpose of this question it has 2 fields

ID (int) PK product ID (F)

I have a standard route set-up

  context.MapRoute ("Product_default", "Product / {controller} / {action} / {id} ", New {controller =" product ", verb =" index ", id = urlparameter. optional));  

and if the URL wants to add an option, then

/ product / option / add / 1

The above URL1 contains ProductID, I have the following code to see the empty model,

  [Add HttpGet] Public ActionSult (return) (see "View"), see "return" New ProductOptionModel () {ProductID = id}); }  

Now in my opinion, I place a hidden field

   x.id )% & Gt;  

It is used to determine (on submitting) if we are editing or adding a new option, however, in the model the binder starts replacing the INID. (Which takes action when moving out of 0) with 1 (or the value of the ID parameter in the URL)

How can I stop or work around this?

View Model

  Public Class Product Product Model {// Database Public ID {Receive; Set; } Public string name {get; Set; } Public int ProductID {get; Set; } Receive public product model product { Set; }}  

I think the id parameter is set Because by default, your route sets it inside your controller, you are setting an extra parameter in your ViewModel of ProductID, it will probably always be equal to the ID parameter, because originally set to QueryString / GET parameter Going (in this case 1).

Your way of changing the route works because you stop it from allocating the ID parameter seems a good option, but it's probably not ideal - it depends on how you You want to solve:

"<"> context.MapRoute ("Product_addoptionalextra", "Product / {Controller} / Add / {ProductID}", New {Controller = "Product", Action = "Add" , ProductID = UrlParameter Optional});

Alternatively, adjust your variables so that the ID is actually the relevant ProductID, you can represent the ID for the other ID.

The way I can suggest if you have MVC 2, fix this problem. Although I do not know your productwhole model , I think the ID is within it. If you set the appropriate template, you can almost forget about the overlapping ID.

  public class ProductExtraModel {// Database Public Int ID {get; Set; } Public string name {get; Set; } [UI Hunt ("Product")] {Receive Public Product Model Product; Set; }}  

When the model is sent back to the controller using productExtraViewModel.Product.ID , you will be able to access the product ID and your normal ID Still available on productViewModel.Id .


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -