MVC2 实体框架 - 更新模型
首先,我是这个星球上唯一尝试将 MVC 与 VB 结合使用的开发人员吗?我搜索了大量的论坛并阅读了很多帖子,每个提出问题的人都给出了一个 C# 示例。不管怎样,既然我已经解决了这个问题,我就得到了一个简单的数据库表(用户),其中包含一些列(用户 ID、用户名、名字、姓氏)。我正在使用实体框架,在编辑视图中,我正在更改值(用户名)并单击“保存”。在我的编辑 http 帖子中,我告诉它返回到索引,但该值未保存。虽然,在我的 http 帖子的构造函数中,我返回了对象并且它显示了新值...但是该值没有进入数据库...有什么帮助吗?
我的控制器:
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>
First of all, am I the only developer on the planet that is attempting to use MVC with VB? I have searched tons of forums and read many posts and everyone that asks a question gives an example in C#. Anyway, now that I've got that out of the way, I've got a simple database table (User) with some columns (UserId, Username, FirstName, LastName). I am using the entity framework and on my edit view, I'm changing a value (Username) and clicking Save. In my Edit http post, I tell it to return to the Index and the value was not saved. Although, in my constructor for the http post, I return the object and it shows the new value...but that value doesn't make it to the db...any help?
My Controller:
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
My view:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,显然我是唯一使用 MVC 的 VB 开发人员。无论如何,我找到了答案。它位于 ASP.Net 站点上的 MVC 初学者教程之一(在这里找到)。答案是更改我的编辑功能 (HttpPost),以便它利用已发布的表单值:
Ok, so, aparently I AM the only VB developer using MVC. Anyway, I found the answer. It was in one of the beginner tutorials for MVC on the ASP.Net Site (Found Here). The answer was to change my Edit function (HttpPost) so that it utilizes the formvalues that were posted: