Passing value from Controller to View in MVC

后端 未结 2 2015
一整个雨季
一整个雨季 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.

    0 讨论(0)
  • 2021-01-25 21:04

    One additional thing to point out is that if you are passing a key value that is stored in the web.config file, say for reCAPTCHA, and the validation fails then you will need to also set the key value in the HttpPost section.

    0 讨论(0)
提交回复
热议问题