2010-11-16 115 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