Asp.net mvc 2 MVC2实体框架-更新模型
首先,我是这个星球上唯一一个尝试将MVC与VB结合使用的开发人员吗?我已经搜索了很多论坛,阅读了很多帖子,每个提出问题的人都给出了一个C语言的例子。无论如何,现在我已经解决了这个问题,我有了一个简单的数据库表用户,其中有一些列UserId、Username、FirstName、LastName。我正在使用实体框架,在我的编辑视图中,我正在更改一个值用户名并单击保存。在我的编辑http帖子中,我告诉它返回到索引,但该值没有保存。虽然,在我的http post构造函数中,我返回了对象,它显示了新的值…但该值并没有进入db…有什么帮助吗 我的控制器:Asp.net mvc 2 MVC2实体框架-更新模型,asp.net-mvc-2,viewmodel,Asp.net Mvc 2,Viewmodel,首先,我是这个星球上唯一一个尝试将MVC与VB结合使用的开发人员吗?我已经搜索了很多论坛,阅读了很多帖子,每个提出问题的人都给出了一个C语言的例子。无论如何,现在我已经解决了这个问题,我有了一个简单的数据库表用户,其中有一些列UserId、Username、FirstName、LastName。我正在使用实体框架,在我的编辑视图中,我正在更改一个值用户名并单击保存。在我的编辑http帖子中,我告诉它返回到索引,但该值没有保存。虽然,在我的http post构造函数中,我返回了对象,它显示了新的值
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>
好的,我是唯一一个使用MVC的VB开发人员。无论如何,我找到了答案。它出现在ASP.Net网站上的一个MVC初学者教程中。答案是更改我的编辑函数HttpPost,以便它使用发布的FormValue:
<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
好的,我是唯一一个使用MVC的VB开发人员。无论如何,我找到了答案。它出现在ASP.Net网站上的一个MVC初学者教程中。答案是更改我的编辑函数HttpPost,以便它使用发布的FormValue:
<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