为什么我的模型无法获得通过查看保存的? [英] Why does my model not get saved by the view?

查看:173
本文介绍了为什么我的模型无法获得通过查看保存的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经作出了一些非常基本的MVC的观点来保存一些数据。到今天为止,我根据他们都在LinqToSql类;现在的我第一次的基础上,我对我自己创建了一个简单的小类的形式,叫做购买

I've been making some very basic MVC views to save some data. To date I've based them all on LinqToSql classes; now for the first time I've based a form on a simple little class I've created on my own, called a Purchase.

public class Purchase {
  long UserID;
  decimal Amount;
  string Description;
}



创建看起来像这样一个观点:

Created a view that looks like this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TestViewer.Models.Purchase>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
  Test Purchase
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  <h2>
    Test Purchase</h2>
  <% using (Html.BeginForm("SavePurchase", "Home")) {%>
  <%: Html.ValidationSummary(true) %>
  <fieldset>
    <legend>Fields</legend>
    <div class="editor-label">
      <%: Html.LabelFor(model => model.UserID) %>
    </div>
    <div class="editor-field">
      <%: Html.DropDownListFor(model => model.UserID, 
    SystemUser.Select(u=>new SelectListItem{ Text=u.UserName, Value=u.ID.ToString()}).OrderBy(s=>s.Text))%>
      <%: Html.ValidationMessageFor(model => model.UserID) %>
    </div>
    <div class="editor-label">
      <%: Html.LabelFor(model => model.Amount) %>
    </div>
    <div class="editor-field">
      <%: Html.TextBoxFor(model => model.Amount) %>
      <%: Html.ValidationMessageFor(model => model.Amount) %>
    </div>
    <div class="editor-label">
      <%: Html.LabelFor(model => model.Description) %>
    </div>
    <div class="editor-field">
      <%: Html.TextBoxFor(model => model.Description) %>
      <%: Html.ValidationMessageFor(model => model.Description) %>
    </div>
    <p>
      <input type="submit" value="Create" />
    </p>
  </fieldset>
  <% } %>
  <div>
    <%: Html.ActionLink("Back to List", "Index") %>
  </div>
</asp:Content>



但是当我回到我的代码的HomeController

public ActionResult SavePurchase(Purchase p) {
   // code here
}

对象 p 有默认值(零和空白)为它的所有字段值。

the object p has default values (zeroes and blanks) for all its field values.

为什么呢?我做了什么错?

Why? What did I do wrong?

推荐答案

在你的购买类使用特性而不是字段(更好的封装),最重要的是他们需要有公开setter方法这个工作:

In your Purchase class use properties instead of fields (for better encapsulation) and most importantly they need to have public setters for this to work:

public class Purchase 
{
    public long UserID { get; set; }
    public decimal Amount { get; set; }
    public string Description { get; set; }
}

在您的例子中,你正在使用的字段,但这些字段的内部,所以你不能指望默认的模型绑定能够将它们设置的值,并且他们只是让你的POST操作里面的默认值。

In your example you are using fields, but those fields are internal so you cannot expect the default model binder being able to set them any values and they just get their default values inside your POST action.

这是说事实上,不被说的那些特性是你有这个代码最小的问题。你的情况最糟糕的事情是,你正在使用你的观点里你LINQ2SQL模型,这是最糟糕反模式我看到人们在ASP.NET MVC应用程序做的一个。你绝对应该使用视图模型。这就是观点应该从控制器传递,这就是控制器应该得到的意见

This being said the fact that those properties are not being said is the smallest problem you have with this code. In your case the worst thing is that you are using your Linq2Sql models inside your views and that's one of the worst anti-patterns I see people doing in ASP.NET MVC applications. You absolutely should be using view models. That's what views should be passed from controllers and that's what controllers should get from views.

这篇关于为什么我的模型无法获得通过查看保存的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆