猿问

如何在 ASP.NET MVC 中预填充表单

在我的情况下,我需要一个模型来表达这样的视图:


@model EditFormApplication.Models.NewForm


@using (Html.BeginForm("EditForm", "Home", FormMethod.Post))

}

   @Html.EditorFor(model => model.Field)

   @Html.EditorFor(model => model.Field)

   @Html.EditorFor(model => model.Field)

   <input type="submit" value="save">

}

@Html.EditorFor(model => model.Field)可能更多,但它们都是一样的。我不知道这种情况的模型应该是什么:


namespace EditFormApplication.Models

{

   public class NewForm

   {

       public string Field { get; set; }

   }

}

我需要将填充的模型发送到 .视图中可以是无限数量的相同输入。Homecontroller


或者最好在不使用的情况下填充模型?EditorFor()


在控制器中,我只需要获得填充的模型


[HttpPost]

public ActionResult EditForm (NewForm model)

{

  return View();

}


慕后森
浏览 82回答 1
1回答

拉风的咖菲猫

您需要在方法中设置模型属性值,如下所示:pre-populateformGET[HttpGet]public ActionResult EditForm(){&nbsp; &nbsp;NewForm newForm = new NewForm()&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; Field = "Your pre-populate text"&nbsp; &nbsp;}&nbsp; &nbsp;return View(newForm);}现在将显示在表单中的每个窗体中。"Your pre-populate text"@Html.EditorFor(model => model.Field)
随时随地看视频慕课网APP
我要回答