按钮重复调用GET方法,编辑时不进行POST

时间:2016-12-02 17:56:50

标签: asp.net-mvc entity-framework asp.net-mvc-4 http-post

我正在尝试编辑我的数据库中的条目,但是当我单击提交时,它只是一遍又一遍地调用GET方法,我无法弄清楚原因。我已经通过断点对此进行了测试,并且没有证据表明POST方法在运行 - 是否与我的制造商绑定有关?

控制器

// GET: Model/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Model model = db.Models.Find(id);
        if (model == null)
        {
            return HttpNotFound();
        }

        ViewBag.Manufacturers = GetManufacturerList(model);

        return View(model);
    }

    // POST: Model/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult EditPost(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        var modelToUpdate = db.Models.Find(id);
        if (TryUpdateModel(modelToUpdate, "",
           new string[] { "ModelName", "ManufacturerID" }))
        {
            try
            {
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch (DataException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
            }
        }

        ViewBag.Manufacturers = GetManufacturerList();

        return View(modelToUpdate);
    }

查看:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Model</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.ModelID)

        <div class="form-group">
            @Html.LabelFor(model => model.ModelName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ModelName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ModelName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Manufacturer.ManufacturerName, "Manufacturer",
                htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-5">
                @Html.DropDownList("ManufacturerID", (List<SelectListItem>)ViewBag.Manufacturers,
                    htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Manufacturer.ManufacturerName, "",
                    new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

型号:

public class Model
{
    [Required]
    [Display(Name = "Manufacturer")]
    [ForeignKey("Manufacturer")]
    public int ManufacturerID { get; set; }

    [Required]
    public int ModelID { get; set; }

    [Required]
    [StringLength(50, ErrorMessage = "Model cannot be longer than 50 characters.")]
    [RegularExpression(@"^[a-zA-Z0-9.-/() ]+$", ErrorMessage = "Invalid characters used. A-Z or a-z, 0-9, '.', '-', '()' and '/' allowed.")]
    [Display(Name = "Model")]
    public string ModelName { get; set; }

    public virtual Manufacturer Manufacturer { get; set; }
    public virtual ICollection<Item> Items { get; set; }
}

public class Manufacturer
{
    [Required]
    public int ManufacturerID { get; set; }

    [Required]
    [StringLength(50, ErrorMessage = "Manufacturer cannot be longer than 50 characters.")]
    [RegularExpression(@"^[a-zA-Z0-9.-/() ]+$", ErrorMessage = "Invalid characters used. A-Z or a-z, 0-9, '.', '-', '()' and '/' allowed.")]
    [Display(Name = "Manufacturer")]
    public string ManufacturerName { get; set; }

    public virtual ICollection<Model> Models { get; set; }
    public virtual ICollection<Item> Items { get; set; }
}

提前致谢。非常感谢任何帮助。

0 个答案:

没有答案