MVC4误解模型与HTTP Post的交互

时间:2013-07-12 18:19:16

标签: c# http asp.net-mvc-4 post

我觉得我想要做的事情应该非常简单(这让我感到愚蠢),但是由于我无法理解,我想我很想知道HTTP POST的方式。

以下是我认为应该如何发生:控制器创建QuoteDimensionViewModel QDVM然后将其发送到视图。 View创建的网页包含模型中各种变量的文本框。用户填写这些文本框。用户单击生成HTTP帖子的buttom,然后使用文本框中的新值更新模型并将其发送到控制器。控制器完成它的工作,我们都过着幸福的生活。

以下是我认为正在发生的事情:当用户点击按钮并且正在创建新的QuoteDimensionViewModel时,只有与它们相关联的文本框的值被填充。因此,在这种情况下,我没有变量Eid的文本框,因此Eid的值在Get中可以是5,但是当Post值始终为0.这会在尝试使用IValidatableObject验证数据时出现问题。 / p>

TLDR; 我基本上想要将带有X变量的模型发送到视图,然后更改这些变量的Y.使用更改验证更新的模型并将其POST回控制器。

感谢您提供任何帮助和建议

首先是模型:

public class QuoteDimensionViewModel
    {
        private QuoteDatabaseEntities Quotedb = new QuoteDatabaseEntities();
        [Required]
        public string ShowerImage { get; set; }
        [Required]
        public EnclosureDimension EnclosureDimensions { get; set; } \\Wrapper class
        [Required]
        public QuoteDimensionsWrapper QuoteDimensions { get; set; } \\Wrapper class
        [Required]
        public int Eid { get; set; }
        [Required]
        public int Sid { get; set; }
        [Required]
        public int Hid { get; set; }
        [Required]
        public int Gid { get; set; }
        public QuoteDimensionViewModel(){ }
        public QuoteDimensionViewModel(int eid, int sid, int hid, int gid)
        {
            Eid = eid;
            Sid = sid;
            Hid = hid;
            Gid = gid;


            this.ShowerImage = (from x in Quotedb.Showers
                           where x.ID == Sid
                           select  x.Picture ).Single();

            this.EnclosureDimensions = (from x in Quotedb.EnclosureDimensions


                                          where (x.Enclosure_ID_FK == Eid)
                                       select x).Single();
            this.QuoteDimensions = new QuoteDimensionsWrapper(Eid);





           }
    }
public class QuoteDimensionsWrapper: IValidatableObject
        {
            public int Eid { get; set; }
            public int ID { get; set; }
            public int Quote_ID_FK { get; set; }
            public double Height { get; set; }
            public double Width { get; set; }

            public QuoteDimensionsWrapper(int eid)
            {
                Eid = eid;
            }
            public QuoteDimensionsWrapper()
            {

                }
            public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
            {
             \\Validation stuff based on the value of Eid
             }

现在控制器:

 public class QuoteDimensionViewModelController : Controller
{

    public ActionResult Quote(int Eid, int Sid, int Hid, int Gid)
    {

        QuoteDimensionViewModel QDVM = new QuoteDimensionViewModel(Eid, Sid, Hid, Gid);

        return View(QDVM);
    }
    [InitializeSimpleMembership]
    [HttpPost]
    public ActionResult Quote(QuoteDimensionViewModel QDVM)
    {
         //Validate the Data then write it to a database

        return RedirectToAction("Index", "QuoteViewModel", new { id = myQid });

    }

}

}

和视图:

@model QuoteWebApp.Models.ViewModels.QuoteDimensionViewModel

@{
    ViewBag.Title = "Quote";
}

<h2>Dimensions</h2>

<img src=@Url.Content(Model.ShowerImage) alt="Image" />

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>QuoteDimensions</legend>

    <div class="editor-label">
        @Html.LabelFor(model =>model.QuoteDimensions.Height)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model =>model.QuoteDimensions.Height)
        @Html.ValidationMessageFor(model =>model.QuoteDimensions.Height)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model =>model.QuoteDimensions.Width)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.QuoteDimensions.Width)
        @Html.ValidationMessageFor(model => model.QuoteDimensions.Width)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model =>model.QuoteDimensions.DoorWidth)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.QuoteDimensions.Width)
        @Html.ValidationMessageFor(model => model.QuoteDimensions.Width)
    </div>     

     }
        <p>
            <input type="submit" value="Quote"  />
        </p>
    </fieldset>
}
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

1 个答案:

答案 0 :(得分:1)

为要从GET持久保存到POST但不希望用户看到的任何值创建隐藏字段。

@Html.HiddenFor(model=>model.Eid)