[英]How to dynamically display a list of fields on asp.net mvc page and pass input value back to back end?
I have a scenario which requires the admin user to identify a number of parameters at the back end and let end user to input value for these parameters, sending back. 我有一种情况,要求管理员用户在后端标识许多参数,然后让最终用户输入这些参数的值,然后发送回去。
I am wondering is there any solution to this kind of scenario? 我想知道对这种情况有什么解决办法吗? My guess, java script would be required. 我的猜测是,需要Java脚本。
Thank you. 谢谢。
You need to make a HTML Form. 您需要制作一个HTML表单。 A form will be rendered to the user, and when the user submits the form, the entered information will be sent back to the server. 表单将呈现给用户,并且当用户提交表单时,输入的信息将发送回服务器。
This page is a decent tutorial on how to make a HTML form using ASP.NET MVC. 本页是有关如何使用ASP.NET MVC制作HTML表单的不错的教程。
You have to create a model (class) with the properties you want the user to submit. 您必须使用要用户提交的属性来创建模型(类)。
public class MyModel
{
public Int32 RandomNumber { get; set; }
}
Then, you will create two MVC actions. 然后,您将创建两个MVC操作。 One, for requesting the form page, which will return a ViewResult
with an empty model. 一种,用于请求表单页面,该页面将返回带有空模型的ViewResult
。 The other will be the submit action, and will require a parameter with the model. 另一个将是Submit操作,并将需要模型的参数。 This will be sent via the user's submission of the form: 这将通过用户提交的表单发送:
public class FooController : Controller
{
[HttpGet]
public void Create()
{
return this.View("~/Views/FooController/Create.cshtml", new MyModel() );
}
[HttpGet]
public void Create(MyModel model)
{
//Do something with model information, such as save to database
}
}
The ViewResult
will need to return a CSHTML page that creates a form for this model. ViewResult
将需要返回一个CSHTML页面,该页面为此模型创建了一个表单。
@model MyModel
@using(Html.BeginForm("Create", "Foo", FormMethod.Post))
{
@Html.TextBoxFor( m => m.RandomNumber )
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.