Pass a simple string from controller to a view MVC3

前端 未结 6 1713
青春惊慌失措
青春惊慌失措 2021-01-31 02:19

I know this seems pretty basic, and it should be, but I cant find out where I am going wrong. (I hve read other articles with similar titles on SO, and other resources on the we

6条回答
  •  执念已碎
    2021-01-31 03:07

    Why not create a viewmodel with a simple string parameter and then pass that to the view? It has the benefit of being extensible (i.e. you can then add any other things you may want to set in your controller) and it's fairly simple.

    public class MyViewModel
    {
        public string YourString { get; set; }
    }
    

    In the view

    @model MyViewModel
    @Html.Label(model => model.YourString)
    

    In the controller

    public ActionResult Index() 
    {
         myViewModel = new MyViewModel();
         myViewModel.YourString = "However you are setting this."
         return View(myViewModel)
    }
    

提交回复
热议问题