TextBoxFor and DisplayFor

(ASP.NET MVC)


Hi,
In this article i am discussing about TextBoxFor and DisplayFor controls in asp.net mvc.

View:-

@{
    ViewBag.Title = "CountryandState";
}

<h2>CountryandState</h2>

@model  ASP.NETMVCFORUMS.ViewModels.CountryandStateViewModel
            <br />

@Html.TextBoxFor(t=>t.countryviewModel.Country)

<br />

@Html.TextBoxFor(t=>t.stateviewModel.State)

@Html.DisplayFor(t => t.stateviewModel.State)


Controller and Model:-

  public ActionResult CountryandState()
        {
          CountryandStateViewModel obj = new CountryandStateViewModel();

          CountryModel obcountry = new CountryModel();
          obcountry.Country = "someCountry";
          StateModel obstate = new StateModel();
          obstate.State = "someState";

          obj.countryviewModel = obcountry;
          obj.stateviewModel = obstate;

          return View(obj);
        }

     public class CountryandStateViewModel
    {
        public CountryModel countryviewModel { get; set; }
        public StateModel stateviewModel { get; set; }
    }
   
     public class CountryModel
    {
        public string Country { set; get; }
    }
   
     public class StateModel
    {
        public string State { get; set; }
    }

Image

Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
7/1/2013
7/1/2013
Download PDF

Comments