避免在控制器 - reg 中使用 viewdata 可枚举对象
我今天有2分 一。我有一个控制器,其中有一个公共静态方法,用于获取诸如
public static List<country> GetCountryLists()
{
List<country> countries = new List<country>();
country _country = new country() { countryname = "Select a country", value = "0" };
country _country1 = new country() { countryname = "India", value = "India" };
country _country2 = new country() { countryname = "USA", value = "USA" };
countries.Add(_country);
countries.Add(_country1);
countries.Add(_country2);
return countries;
}
“当前”之类的复选框的详细信息,我正在通过使用此函数
ViewData["country"] = GetCountryLists();
,我可以在视图中使用与此相同的函数吗?不需要使用 viewdata 对象,
<%: Html.DropDownList("country", new SelectList(UserController.GetCountryLists(), "value", "countryname", "0"))%>
请建议我最佳实践。
二.当我使用相同的 id 和 id 时,我还有另一个查询;单选按钮的名称,客户端的验证工作正常。 如果我对一组复选框使用相同的条件,则在客户端验证期间不会突出显示复选框,只有在服务器验证期间,我才会收到错误消息,但控件 [复选框] 没有指示错误的红色边框。 我已经使用自己的 html 帮助器按照 http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs
生成复选框列表,
请告诉我是否有这个问题也有任何可能的解决方案。由于我是 ASP.NET MVC2 的新手,我不确定是否使用这些..请相应地建议我。
I have 2 points for today
I. I have a controller in which i have a public static method for getting the details for a checkbox like
public static List<country> GetCountryLists()
{
List<country> countries = new List<country>();
country _country = new country() { countryname = "Select a country", value = "0" };
country _country1 = new country() { countryname = "India", value = "India" };
country _country2 = new country() { countryname = "USA", value = "USA" };
countries.Add(_country);
countries.Add(_country1);
countries.Add(_country2);
return countries;
}
Currently , i am using this function via
ViewData["country"] = GetCountryLists();
is it ok for me to use this same function like this one in the view, so that I do not need to use the viewdata object,
<%: Html.DropDownList("country", new SelectList(UserController.GetCountryLists(), "value", "countryname", "0"))%>
Kindly suggest me the best practice.
II. Also i have another query, when i use the same id & name for the radiobuttons, validation at the client side is working fine.
If I use the same condition for a group of checkboxes, i get do not get the checkboxes highlighted during client validation and only during server validation, i get the error message, but the controls [checkboxes] do not have a red border indicating the error.
I have used my own html helper to generate the checkboxlist as per http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs
Kindly let me know if there is any possible solution for this problem too. As i am new to asp.net mvc2, i am not sure of using these.. kindly suggest me accordingly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
?不,这不是一个好的实践,并且由于各种原因违反了 MVC 模式。视图不应该提取信息。他们应该只使用在视图模型中传递的信息。控制器负责调用各种方法来获取数据,然后构造一个包含视图所需的所有必要信息的视图模型,然后将此视图模型传递给视图。所以这里是建议的方法:
或者使用丑陋/弱类型/避免使用/需要魔术字符串
ViewData
:控件有责任填充视图模型属性或丑陋的ViewData。
就您的第二个问题而言,您需要显示您的代码才能查看它有什么问题。根据你的描述,我可以说的是,在 DOM 中不能有两个具有相同 id 的元素,否则你会得到无效的标记。
No, this is not good practice and violation of the MVC pattern for various reasons. Views shouldn't pull information. They should only use the information that it is being passed in the view model. It is the controller's responsibility to invoke various methods to fetch data and then construct a view model containing all the necessary information needed for the view and then pass this view model to the view. So here's the suggested way:
or with the ugly/weakly typed/avoid to use/requiring magic strings
ViewData
:and it is the responsibility of the control to populate either the view model properties or the ugly ViewData.
As far as your second question is concerned you will need to show your code in order to see what is wrong with it. From your description what I can say is that you cannot have two elements with the same id in the DOM or you get invalid markup.