Articles → ASP .NET MVC → Model in asp.net MVC

Model in asp.net MVC






Purpose





Example

















Create a model class




public class TestModel
{

}



Create a property and a method inside the model class




public class TestModel
{
        public string TestProperty{ get; set; }

        public string GetSomething()
        {
            return "Test";
        }
}





Create a strongly type view





Click to Enlarge



  public ActionResult Test()
  {

            ViewBag.Message = "Your app description page.";

            return View();
  }



Call the method created in the model




public ActionResult Test()
{
            TestModel model = new TestModel();
            model.TestProperty = model.GetSomething();

            ViewBag.Message = "Your app description page.";

            return View(model);
}



Bind data with a textbox




@model MvcApplication1.Models.TestModel

@{
    ViewBag.Title = "Test";
}

@Html.TextBoxFor(x => x.TestProperty)





Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Sunday, January 01, 2017

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250