如何在 mvc 的文本框中设置服务器端的动态文本

本文关键字:文本 设置 服务器端 动态 mvc | 更新日期: 2023-09-27 18:34:20

我正在使用带有Razor视图引擎的MVC 4。在这里,我可以从客户端到服务器端获取值,但现在我想设置数据库值以绑定受尊重的控件,服务器端到客户端。但是我们怎么能做到这一点呢???

@using (Html.BeginForm("Registration", "Home"))
{
    @Html.Label("User Name: "); @Html.TextBox("txtUserName","");
    @Html.Label("Password: "); @Html.TextBox("txtPassword", "");
    @Html.Label("Email ID: "); @Html.TextBox("txtEmailID", "");
    @Html.Label("Age: "); @Html.TextBox("txtAge", "");
    @Html.Label("Adderss: "); @Html.TextBox("txtAdderss", "");
    @Html.Label("Gender: "); @Html.TextBox("txtGender", "");
       <input type="button" value="Update" />  
}

如何在 mvc 的文本框中设置服务器端的动态文本

使用模型。将包含值的模型从服务器端发送到客户端。

 //in the model
public class User
{
public string txtUserName { get; set; }
public string txtPassword { get; set; }
public string txtEmailID { get; set; }
public string txtAge { get; set; }
public string txtAdderss { get; set; }
public string txtGender { get; set; }
}
//in the controller
public ActionResult Registration()
{
User userObj = new User(); 
// or you can make a database call and fill the model object 
userObj.txtUserName = "Name";
userObj.txtPassword = "password";
userObj.txtEmailID = "email";
userObj.txtAge = "age";
userObj.txtAdderss = "address";
userObj.txtGender = "gender";
return View(userObj);
}
//in the view
@model Model.User
@using (Html.BeginForm("Registration", "Home"))
{
@Html.Label("User Name: "); @Html.TextBoxFor(model => model.txtUserName);
@Html.Label("Password: "); @Html.TextBoxFor(model => model.txtPassword);
@Html.Label("Email ID: "); @Html.TextBoxFor(model => model.txtEmailID);
@Html.Label("Age: "); @Html.TextBoxFor(model => model.txtAge);
@Html.Label("Adderss: "); @Html.TextBoxFor(model => model.txtAdderss);
@Html.Label("Gender: ");  @Html.TextBoxFor(model => model.txtGender);
<input type="button" value="Update" /> }
 

'

此链接可能会帮助您了解 MVC 基础知识

链接

您必须使用模型将值从控制器传递到视图。快速的谷歌搜索返回了这个网站,其中包含将数据从控制器传递到视图的示例。