2010-11-16 114 views
1

首先,我是正在尝试使用MVC与VB这个星球上唯一的开发商?我搜索了大量的论坛并阅读了许多帖子,每个提出问题的人都在C#中给出了一个例子。无论如何,现在我已经知道了,我有一个简单的数据库表(用户)与一些列(UserId,用户名,名,姓)。我正在使用实体框架,并在我的编辑视图中,我正在更改一个值(用户名)并单击保存。在我编辑http文章中,我告诉它返回到索引并且该值没有保存。虽然,在我的http post的构造函数中,我返回对象并显示新的值...但该值不会使数据库...任何帮助?MVC2实体框架 - 更新模型

我的控制器:

Function Edit(ByVal ID As Guid) As ActionResult 
     'get the user 
     Dim usr = (From u In db.Users 
        Where u.UserId = ID 
        Select u).Single 

     Return View(usr) 
    End Function 

    <HttpPost()> _ 
    Function Edit(ByVal ID As Guid, ByVal usrInfo As User, ByVal formValues As FormCollection) As ActionResult 
     '   Dim usr As User = db.Users.Single(Function(u) u.UserId = ID) 

     If ModelState.IsValid Then 
      TryUpdateModel(usrInfo, "User") 

      Return RedirectToAction("Index") 
     Else 
      Return View(usrInfo) 
     End If 
    End Function 

我的观点:

<h2>Edit</h2> 

<%-- The following line works around an ASP.NET compiler warning --%> 
<%: ""%> 

<% Using Html.BeginForm() %> 
    <%: Html.ValidationSummary(True) %> 
    <fieldset> 
     <legend>Fields</legend> 

     <div class="editor-label"> 
      <%: Html.LabelFor(Function(model) model.UserId) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(Function(model) model.UserId) %> 
      <%: Html.ValidationMessageFor(Function(model) model.UserId) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(Function(model) model.Username) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(Function(model) model.Username) %> 
      <%: Html.ValidationMessageFor(Function(model) model.Username) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(Function(model) model.FirstName) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(Function(model) model.FirstName) %> 
      <%: Html.ValidationMessageFor(Function(model) model.FirstName) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(Function(model) model.LastName) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(Function(model) model.LastName) %> 
      <%: Html.ValidationMessageFor(Function(model) model.LastName) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(Function(model) model.CreatedDate) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(Function(model) model.CreatedDate, String.Format("{0:g}", Model.CreatedDate)) %> 
      <%: Html.ValidationMessageFor(Function(model) model.CreatedDate) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(Function(model) model.CreatedBy) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(Function(model) model.CreatedBy) %> 
      <%: Html.ValidationMessageFor(Function(model) model.CreatedBy) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(Function(model) model.LastLogin) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.TextBoxFor(Function(model) model.LastLogin, String.Format("{0:g}", Model.LastLogin)) %> 
      <%: Html.ValidationMessageFor(Function(model) model.LastLogin) %> 
     </div> 

     <p> 
      <input type="submit" value="Save" /> 
     </p> 
    </fieldset> 

<% End Using %> 

<div> 
    <%: Html.ActionLink("Back to List", "Index") %> 
</div> 

回答

1

确定,所以,aparently我使用MVC的唯一VB开发人员。无论如何,我找到了答案。这是ASP.Net站点上的MVC初学者教程之一(Found Here)。答案是改变我的编辑功能(HttpPost),以便它利用张贴的表单值:

<HttpPost()> _ 
    Function Edit(ByVal id As Guid, ByVal collection As FormCollection) As ActionResult 
     Dim rest = (From r In db.Restaurants 
       Where r.RestaurantId = id 
       Select r).Single() 

     Try 

      TryUpdateModel(rest, collection.ToValueProvider()) 

      db.SaveChanges() 

      Return RedirectToAction("Index") 
     Catch 
      Return View(rest) 
     End Try 
    End Function