开发者

avoiding the use of the viewdata enumerable object in controller - reg

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 prac开发者_如何学Ctice.

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.


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

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:

<%= Html.DropDownListFor(
    x => x.Country, 
    new SelectList(Model.Countries, "value", "countryname")
) %>

or with the ugly/weakly typed/avoid to use/requiring magic strings ViewData:

<%= Html.DropDownList(
    "Country", 
    new SelectList((IEnumerable)ViewData["Countries"], "value", "countryname")
) %>

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜