Html.EditorFor Set Default Value

后端 未结 12 1894
夕颜
夕颜 2020-12-13 03:44

Rookie question. I have a parameter being passed to a create view. I need to set a field name with a default value. @Html.EditorFor(model => model.Id) I need to set this in

12条回答
  •  孤街浪徒
    2020-12-13 04:08

    The clean way to do so is to pass a new instance of the created entity through the controller:

    //GET
    public ActionResult CreateNewMyEntity(string default_value)
    {
        MyEntity newMyEntity = new MyEntity();
        newMyEntity._propertyValue = default_value;
    
        return View(newMyEntity);
    }
    

    If you want to pass the default value through ActionLink

    @Html.ActionLink("Create New", "CreateNewMyEntity", new { default_value = "5" })
    

提交回复
热议问题