Articles → ASP .NET MVC → Model In Asp.Net MVC

Model In Asp.Net MVC






Purpose





Example




  1. Create a model class
  2. Create a property and a method inside the model class.
  3. Create a strongly type view.
  4. Call the method created in the model.
  5. Bind data with a textbox.



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




Picture showing creating 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)




Picture showing the output of Model in asp.net MVC
Click to Enlarge


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

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250