Naming ASP.NET Controls Inside User Controls with ASP.NET MVC

I am wondering if there is a way to make ASP.NET controls work well with my ASP.NET MVC application. That's what I'm doing.

I have an order page that displays information about one Order object. There will usually be many rows of data on a page, each row representing an OrderItem object. Each row represents an ASP.NET custom control. The user control has a form element with two text boxes (quantity and price) and an update button.

When I click the refresh button, I expect the form to submit data for that single OrderItem row to the controller method and update the OrderItem record in the database.

Here is my problem. When the message occurs, the framework complains because the fields in the form do not match the parameters in the controller method. Each form field is something like "OrderItem_1 $ Quantity" or "OrderItem_2 $ Price" instead of "Quantity" or "Price" which will correspond to my method parameters.

I was told that I can overcome this by making sure the IDs of all my controls are unique to the page, but allow the NAME to be repeated between different forms, so that if I expose the form on a separate line, that name could be something that matches the method my controller.

The only problem is that I am using ASP.NET controls for my textboxes (which I REALLY want to keep doing) and I can't find a way to override the name field. There is no name on the ASP.NET control and even when I try to set it using the accessributes property of the attributes by saying "control.Attributes [" Name "] =" Price "; it just adds another name = attribute to the HTML tag. which doesn't work.

Does anyone know how I can do this? I really don't like all the HtmlHelper features like TextBox and DropDown, because I hate that my .aspx has to be PHP or ASP like with <%%> tags etc. Thank!

0


source to share


1 answer


I think you have straddled the two worlds of ASP.NET WebForms and ASP.NET MVC. You really need to use the methods of Html.TextBox, etc. In MVC. This gives you complete control over the markup, which is one of the main advantages of MVC.

The very problem you are having with control over the generated HTML for example. getting two name attributes is exactly what MVC is for. If you stop fighting it and go with the flow it will work much better.



Tags

<%%> are not a problem if you don't have logic. Setting a simple presentation logic is fine in your opinion.

If you don't like that, it might be better to stick with the standard ASP.NET.

+3


source







All Articles