2013-02-07 49 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

相关问题