使用ASP.NET MVC命名用户控件中的ASP.NET控件

时间:2022-02-11 16:49:35

I am wondering if there is a way to make ASP.NET controls play nicely with my ASP.NET MVC app. Here is what I am doing.

我想知道是否有办法使ASP.NET控件与我的ASP.NET MVC应用程序很好地配合。这就是我在做的事情。

I have an order page which displays info about a single Order object. The page will normally have a bunch of rows of data, each row representing an OrderItem object. Each row is an ASP.NET User Control. On the user control there is a form element with two text boxes (Quantity and Price), and an update button.

我有一个订单页面,显示有关单个订单对象的信息。该页面通常有一堆数据行,每行代表一个OrderItem对象。每行都是一个ASP.NET用户控件。在用户控件上有一个表单元素,其中包含两个文本框(数量和价格)和一个更新按钮。

When I click the update button, I expect the form to post the data for that individual OrderItem row to a controller method and update the OrderItem record in the database.

当我单击更新按钮时,我希望表单将该单个OrderItem行的数据发布到控制器方法并更新数据库中的OrderItem记录。

Here is my problem: When the post happens, the framework complains because the fields on the form don't match the parameters on the controller method. Each form field is something like "OrderItem_1$Quantity" or "OrderItem_2$Price" instead of just "Quantity" or "Price" which would match my method parameters.

这是我的问题:当帖子发生时,框架会抱怨,因为表单上的字段与控制器方法上的参数不匹配。每个表单字段类似于“OrderItem_1 $ Quantity”或“OrderItem_2 $ Price”,而不仅仅是“Quantity”或“Price”,它们与我的方法参数匹配。

I have been told that I can overcome this by making sure that the IDs of all my controls are unique for the page, but allow the NAMEs to be repeated between different forms, so that if a form for an individual row is posted, the name can be something that will match what is on my controller method.

有人告诉我,我可以通过确保所有控件的ID对于页面都是唯一的,但允许在不同表单之间重复NAME来解决这个问题,这样如果发布了单个行的表单,名称可以是与我的控制器方法相匹配的东西。

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

唯一的问题是我使用ASP.NET控件作为我的文本框(我真的想继续这样做),我找不到任何方法来覆盖名称字段。 ASP.NET控件上没有Name属性,即使我尝试使用Attributes accessor属性设置它,也可以说“control.Attributes [”Name“] =”Price“;”它只是为HTML标记添加了另一个name =属性,它不起作用。

Does any one know how I can make this work? I really don't like all of the HtmlHelper functions like TextBox and DropDown because I hate having my .aspx be so PHP or ASP like with the <%%> tags and everything. Thanks!

有谁知道我怎么做这个工作?我真的不喜欢所有的HtmlHelper函数,如TextBox和DropDown,因为我讨厌让我的.aspx像PHP或ASP那样使用<%%>标签和所有内容。谢谢!

1 个解决方案

#1


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

我认为你跨越了两个ASP.NET WebForms和ASP.NET MVC世界。你真的需要在MVC中使用Html.TextBox方法等。这使您可以完全控制标记,这是MVC的主要优点之一。

The very problem you're having with control over the generated HTML, e.g. getting two name attributes, is exactly what MVC is designed to address. If you stop fighting it and go with the flow, it'll work much better.

您控制生成的HTML时遇到的问题,例如:获得两个名称属性,正是MVC旨在解决的问题。如果你停止战斗并顺其自然,它会更好地运作。

<% %> tags aren't a problem unless you have logic in there. Putting simple presentation logic on your view is fine.

除非你有逻辑,否则<%%>标签不是问题。在您的视图上放置简单的表示逻辑很好。

If you don't like this, then maybe it's better to stick with standard ASP.NET.

如果你不喜欢这个,那么最好坚持使用标准的ASP.NET。

#1


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

我认为你跨越了两个ASP.NET WebForms和ASP.NET MVC世界。你真的需要在MVC中使用Html.TextBox方法等。这使您可以完全控制标记,这是MVC的主要优点之一。

The very problem you're having with control over the generated HTML, e.g. getting two name attributes, is exactly what MVC is designed to address. If you stop fighting it and go with the flow, it'll work much better.

您控制生成的HTML时遇到的问题,例如:获得两个名称属性,正是MVC旨在解决的问题。如果你停止战斗并顺其自然,它会更好地运作。

<% %> tags aren't a problem unless you have logic in there. Putting simple presentation logic on your view is fine.

除非你有逻辑,否则<%%>标签不是问题。在您的视图上放置简单的表示逻辑很好。

If you don't like this, then maybe it's better to stick with standard ASP.NET.

如果你不喜欢这个,那么最好坚持使用标准的ASP.NET。