Passing value from Controller to View in MVC

后端 未结 2 2022
一整个雨季
一整个雨季 2021-01-25 20:07

I have a view that was generated using data scaffolding. The view has a textfield:

Create View:

     
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-25 20:58

    You could do this by using the ViewBag to store GroupId as you currently are, but as your view is using a model, it may be better to go that way instead.

    So you would have:

    Model:

    public class GroupModel
    {
        public int GroupId { get; set; }
    }
    

    Controller:

    public ActionResult Create(int id)
    {
        var model = new GroupModel();
        model.GroupId = id
    
        return View(model);
    }
    

    At the top of your view, you would then reference your model:

    @model GroupModel
    

    You can then access the model in the view using code like model.GroupId as per the scaffolding has done.

提交回复
热议问题