2013-02-07 75 views
0

我有一個文本框,用戶可以在其中輸入所需的用戶名並保存它。一旦他們保存了,他們碰巧重新訪問他們的個人資料頁面,該文本框應該填入他們保存的最後一個用戶名,並且用戶仍然可以改變它並重新保存。我對此很新,不確定如何正確開始。我正在使用vs 2012 asp.net mvc 4 c#。這是我到目前爲止的代碼:將文本框字符串值保存到數據庫c#

@model School.Models.StudentNameModel 

    @using (Html.BeginForm("_StudentNamePartial", "Profile")) { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary() 
<fieldset> 
    <ol> 
     <li> 
      @Html.LabelFor(m => m.StudentName) 
      @Html.DisplayFor(m => m.StudentName) 
      @Html.TextBoxFor(m=>m.StudentName) 
      <button type="button" value="save" /> 
     </li> 
    </ol> 

</fieldset> 

}

這是我的模型:

public class StudentNameModel 
{ 
    [Display(Name = "Student Name")] 
    public string StudentName{ get; set; } 
} 

我的控制器:

GET - 從數據庫中獲取學生姓名(如果存在) 。

[HttpPost] 
    public ActionResult _StudentNamePartial(int id) 
    { 
     id = WebSecurity.CurrentStudentId; 
     var model = new StudentNameModel(); 
     using (var db = new StudentsDataContext()) 
     { 
      var result = (from u in db.Students 
         where u.ID == id 
         select u.StudentName).FirstOrDefault(); 
      if(result != null) 
       model.StudentName= result; 
     } 
     return View(model); 
    } 

POST - 這就是我要保存新的用戶名我有麻煩的是,當我顯示的用戶名是不是打我的Action方法,它的學生

[HttpPost] 
    public ActionResult _StudentNamePartial(StudentNameModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      using (var db = new StudentDataContext()) 
      { 
       try 
       { 

       } 
       catch (Exception) 
       { 

        throw; 
       } 
      } 
      return RedirectToAction("ProfileAccount"); 
     } 
     return View(model); 
    } 

而且始終報告對象引用爲空。任何幫助都會很棒。感謝:D

+1

你有,你說這是一個'GET'了'HttpPost'你的控制器action屬性。另外,你不應該使用'Html.DisplayFor',因爲你沒有使用模板。 – CAbbott

+0

@CAbbott - 我刪除了'HttpPost',但我仍然沒有得到一個值顯示。而不是'Html.DisplayFor'我應該使用什麼?我試過'@(Model.StudentName)'但我總是得到對象的錯誤爲空。爲什麼它不符合我的Action方法? – 072et

+0

@CAbbott我有這個作爲局部視圖,以便用戶名部分在名爲'@ Html.Partial(「_ StudentNamePartial」)''的視圖中,然後顯示模型並通過@using(Html獲取名稱。 BeginForm(「_ StudentNamePartial」,「Profile」)){..}' – 072et

回答

0

看起來你試圖從控制器操作呈現局部視圖作爲大視圖的一部分。在這種情況下,部分視圖應該在ProfileAccount視圖內呈現。

您可以構建控制器和視圖這樣的(大致的輪廓):

ProfileAccount視圖模型

public class ProfileAccountView 
{ 
    public StudentNameModel StudentName { get; set; } 
} 

檔案控制器

[HttpGet] 
public ActionResult ProfileAccount(int id) 
{ 
    // Get whatever info you need and store in a ViewModel 
    var model = new ProfileAccountView(); 

    // Get the student info and store within ProfileAccountView 
    // Do your database reads 
    model.StudentName = new StudentNameModel { StudentName = result }; 

    return View(model); 
} 

[HttpPost] 
public ActionResult ProfileAccount(ProfileAccountView profile) 
{ 
    // Do whatever processing here 
} 

ProfileAccount查看

@model School.Models.ProfileAccountView 

@using (Html.BeginForm("ProfileAccount", "Profile")) 
{ 
    @Html.RenderPartial('_StudentNamePartial', Model.StudentName); 
    <button type="button" value="save" /> 
} 

_StudentNamePartial管窺

@model School.Models.StudentNameModel 

<fieldset> 
    <ol> 
     <li> 
      @Html.LabelFor(m => m.StudentName) 
      @Html.TextBoxFor(m=>m.StudentName) 
     </li> 
    </ol> 
</fieldset> 
+0

我也有一個問題 - 我有一個剃刀視圖頁面和每個視圖文件我只能有一個模型 - 我如何創建一個視圖頁面,將持有多個部分視圖每個部分組成整個網頁的一部分。 – 072et

+0

我的答案設置了。你爲你的「main」視圖創建一個ViewModel,它保存ViewModel中所有你想從主視圖渲染的部分視圖。 – CAbbott

相關問題