更新模型后,这些值不会反映在视图上

发布于 2024-09-26 04:23:36 字数 999 浏览 1 评论 0原文

我使用 Visual Web Developer 2010 和 ASP .NET 与 MVC2 创建了一个简单的客户管理模块,其中视图将允许输入名字、姓氏、地址等,提交后,将分配一个新的“ID” CustomerModel 实例的 CustomerID 属性。

用于创建客户的控制器方法(CustomerController)如下:

<HttpPost()> _
Public Function CreateCustomer(ByVal model As CustomerModel) As ActionResult
    model = Me.customerServiceClientValue.CreateCustomer(model)
    Return (View(model))
End Function

我可以看到控件到达了CreateCustomer()方法内部,并且它调用客户服务来创建客户。该服务还会向客户返回一个新 ID。正如在该方法中一样,从服务调用返回的客户实例被分配回传入的“模型”变量。该模型实例被传递到返回语句中的“视图”。然而,当刷新视图时,我没有看到客户 ID 字段的值。

我的客户 ID 字段有以下标记:

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

我做错了什么?尽管模型包含新的客户 ID,为什么我在字段中没有获取客户 ID?感谢您的所有帮助。

谢谢和问候,

迪内什·贾亚德万

I used Visual Web Developer 2010 and ASP .NET with MVC2 to create a simple Customer Management module, where the view will allow entering first name, last name, address, etc., and upon submitting, a new "ID" will be assigned to the CustomerID property of the CustomerModel instance.

The Controller method (CustomerController) for creating customer is as below:

<HttpPost()> _
Public Function CreateCustomer(ByVal model As CustomerModel) As ActionResult
    model = Me.customerServiceClientValue.CreateCustomer(model)
    Return (View(model))
End Function

I can see that the control reaches inside the CreateCustomer () method, and it calls the Customer Service to create the customer. The service returns a customer with a new ID as well. As in the method, the customer instance returned from the service call is assigned back to the "model" variable that came in. That model instance is passed to the "View" in the return statement. Yet, when the view is refreshed, I do not see a value for the customer ID field.

I have the following mark-up for the customer ID field:

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

What am I doing wrong? Why am I not getting the Customer ID in the field, although the model contains the new Customer ID? Appreciate all your helps.

Thanks and regards,

Dinesh Jayadevan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埋葬我深情 2024-10-03 04:23:36

因为 TextBoxFor (和其他帮助程序)始终首先查看要显示的发布值。如果不存在后期值,则它会查看模型。这样做的原因是,如果您的表单有错误,您通常希望表单显示与发布的数据相同的数据,而不是一些可能已更改的模型数据。

您可以在控制器中调用 ModelState.Clear(); 来防止这种情况发生,但这不是一个好主意。

有关此的更多信息
http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx

Because TextBoxFor (and other helpers) always looks at the post values first to display. If no post value exists then it looks at the model. The reason for this is because if your form has errors your generally want your form to show the same data as was posted rather than some, possibly altered, model data.

You can call ModelState.Clear(); in your controller to prevent that but it's not a good idea.

More info on this
http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文