C# 如何在mvc的文本框中设置服务器端的动态文本
我正在使用MVC4和Razor视图引擎。在这里,我可以从客户端到服务器端获取值,但现在我想设置数据库值以绑定受尊重的控件,从服务器端到客户端。但我们如何才能做到这一点C# 如何在mvc的文本框中设置服务器端的动态文本,c#,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,我正在使用MVC4和Razor视图引擎。在这里,我可以从客户端到服务器端获取值,但现在我想设置数据库值以绑定受尊重的控件,从服务器端到客户端。但我们如何才能做到这一点 @using (Html.BeginForm("Registration", "Home")) { @Html.Label("User Name: "); @Html.TextBox("txtUserName",""); @Html.Label("Password: "); @Html.TextBox("txtPa
@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" />
}
@使用(Html.BeginForm(“注册”、“主页”))
{
@Html.Label(“用户名:”;@Html.TextBox(“txtUserName”,”);
@Html.Label(“Password:”;@Html.TextBox(“txtPassword”,”);
@Html.Label(“电子邮件ID:”);@Html.TextBox(“txtEmailID”,”);
@Html.Label(“Age:”);@Html.TextBox(“txtAge”,”);
@Html.Label(“Adderss:”);@Html.TextBox(“txtAdderss”,”);
@Html.Label(“性别:”;@Html.TextBox(“txtGender”,”);
}
您必须使用模型从控制器向视图传递值。
google快速搜索返回此站点,其中包含从控制器向视图传递数据的过程。使用模型。将带有值的模型从服务器端发送到客户端
//在模型中
公共类用户
{
公共字符串txtUserName{get;set;}
公共字符串txtPassword{get;set;}
公共字符串txtEmailID{get;set;}
公共字符串txtAge{get;set;}
公共字符串txtAdderss{get;set;}
公共字符串txtGender{get;set;}
}
//在控制器中
公众行动结果登记()
{
User userObj=新用户();
//也可以进行数据库调用并填充模型对象
userObj.txtUserName=“Name”;
userObj.txtPassword=“password”;
userObj.txtEmailID=“电子邮件”;
userObj.txtAge=“年龄”;
userObj.txtAdderss=“地址”;
userObj.txtGender=“性别”;
返回视图(userObj);
}
//在视图中
@模型用户
@使用(Html.BeginForm(“注册”、“主页”))
{
@Html.Label(“用户名:”;@Html.TextBoxFor(model=>model.txtUserName);
@Label(“Password:”;@Html.TextBoxFor(model=>model.txtPassword);
@Label(“Email ID:”);@Html.TextBoxFor(model=>model.txtEmailID);
@Html.Label(“Age:”;@Html.TextBoxFor(model=>model.txtAge);
@Label(“Adderss:”);@Html.TextBoxFor(model=>model.txtAdderss);
@Html.Label(“性别:”;@Html.TextBoxFor(model=>model.txtGender);
}
`
此链接可能会帮助您了解mvc基础知识
Thankx Brother,你的链接确实帮了我的忙,但我正在文本框中查找绑定数据。
//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" /> }