2016-02-12 55 views
1

我正在使用MVC 4和Razor視圖引擎。在這裏,我可以從客戶端獲取值到服務器端,但現在我想設置數據庫值來綁定受尊重的控件,服務器端到客戶端。但我們怎麼能做到這一點....?如何在mvc文本框中設置服務器端的動態文本

@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" /> 
} 

回答

1

使用模型。將模型的值從服務器端發送到客戶端。

//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基本

link

0

您將不得不使用模型將值從Controller傳遞到您的視圖。 快速谷歌搜索返回此網站與examples從控制器傳遞數據查看。

+0

Thankx兄弟,你的鏈接真正幫助我,但我一直在尋找文本框綁定數據。 –

相關問題