1

我觉得我想做的事情应该很容易(这让我感觉很愚蠢),但由于我无法弄清楚,我想我想念如何理解 HTTP POST。

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

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

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")
}
4

1 回答 1

1

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

@Html.HiddenFor(model=>model.Eid)
于 2013-07-12T19:25:37.843 回答