简体   繁体   English

MVC视图页面提交不会返回到控制器

[英]MVC view page submit does not back to the controller

The issue for below coding is when I click "Submit" button, I expect it can bring me from view page back to the controller. 下面编码的问题是当我单击“提交”按钮时,我希望它可以将我从视图页面带回到控制器。 I set an break point at controller method [HttpPost] ProductEdit, it never stop there. 我在控制器方法[HttpPost] ProductEdit上设置了一个断点,它从未停止过。

The view generate the code :
<form method="post" action="/Home/ProductEdit?ProductId=4" novalidate="novalidate">
<input type="submit" value="Submit">

Please help me figure out the reason and provide detail code to fix it. 请帮助我找出原因,并提供详细代码进行修复。


public class ProductEditViewModel : Product
{
    // For DropDownListFor need IEnumerable<SelectListItem>
    public IEnumerable<SelectListItem> SupplierItems { get; set; }

    // For RadioButtonFor need below
    public Int32? CategorySelectedId { get; set; }
    public IEnumerable<Category> CategorieItems { get; set; }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Web.Script.Serialization;
using MvcApplication1.Models;
using System.Data.Objects.SqlClient;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            var q = Quize(null);

            return View();
        }

        [HttpGet]
        public ActionResult ProductEdit(Int32 ProductId)
        {
            var northwind = new NorthwindEntities();

            var q = from p in northwind.Products
                    where p.ProductID == ProductId
                    select new ProductEditViewModel
                    {
                        ProductID = p.ProductID,
                        ProductName = p.ProductName,
                        UnitPrice = p.UnitPrice,

                        SupplierItems = from sup in northwind.Suppliers
                                        select new SelectListItem
                                            {
                                                Text = sup.CompanyName,
                                                Value = SqlFunctions.StringConvert((double)sup.SupplierID),
                                                Selected = sup.SupplierID == p.SupplierID
                                            },

                        CategorySelectedId = p.CategoryID,
                        CategorieItems = from cat in northwind.Categories select cat,

                        Discontinued = p.Discontinued
                    };

            return View(q.SingleOrDefault());
        }

        [HttpPost]
        public ActionResult ProductEdit(ProductEditViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }
            return View(viewModel);
        }
    }
}

@model MvcApplication1.Models.ProductEditViewModel

@{
    Layout = null;
}

<script type="text/javascript">

</script>

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ProductEdit</title>
</head>
<body>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>


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

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

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

            <div class="form-group">
                @Html.LabelFor(model => model.SupplierID, "SupplierID", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @*DropDownList is for deiplay purpose*@
                    @*@Html.DropDownList("SupplierID", Model.SupplierItems, htmlAttributes: new { @class = "form-control" })*@

                    @*DropDownListFor is for Edit purpose*@
                    @Html.DropDownListFor(model => model.SupplierID, Model.SupplierItems, htmlAttributes: new { @class = "form-control" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.CategoryID, "CategoryID", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @*RadioButton is for Display purpose*@
                    @*@foreach (var Categorie in Model.CategorieItems)
                    {
                        @Html.RadioButton("CategoryID", Categorie.CategoryID, Model.CategorySelectedId == Categorie.CategoryID) @Categorie.CategoryName; @:&nbsp;&nbsp;&nbsp;
                    }*@

                    @*RadioButtonFor is for Edit purpose*@
                    @foreach (var Categorie in Model.CategorieItems)
                    {
                        @Html.RadioButtonFor(model => model.CategorySelectedId, Categorie.CategoryID) @Categorie.CategoryName; @:&nbsp;&nbsp;&nbsp;
                    }

                </div>
            </div>

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

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

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

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
</body>
</html>

The issue (View Page does not post back to [HttpPost] Controller) cause by below line code: 由以下行代码引起的问题(“查看页面不会回发到[HttpPost]控制器”):

Value = SqlFunctions.StringConvert((double)sup.SupplierID),

The correction code is : 更正代码为:


[HttpGet]
public ActionResult ProductEdit(Int32 ProductId)
{
    var northwind = new NorthwindEntities();

    var q = from p in northwind.Products.AsEnumerable()  //to enumerate all records in memory and then use ToString
            where p.ProductID == ProductId
            select new ProductEditViewModel
            {
                Product = p,

                SupplierItems = from sup in northwind.Suppliers.AsEnumerable()  //to enumerate all records in memory and then use ToString
                                select new SelectListItem
                                    {
                                        Text = sup.CompanyName,
                                        Value = sup.SupplierID.ToString(),  //to enumerate all records in memory and then use ToString
                                        Selected = sup.SupplierID == p.SupplierID
                                    },

                CategorySelectedId = p.CategoryID,
                CategorieItems = from cat in northwind.Categories.AsEnumerable() select cat,  //to enumerate all records in memory and then use ToString
            };

    return View(q.SingleOrDefault());
}

[HttpPost]
public ActionResult ProductEdit(ProductEditViewModel vm)
{
    return View();
}

public class ProductEditViewModel
{
    public Product Product;

    // For DropDownListFor need IEnumerable<SelectListItem>
    public IEnumerable<SelectListItem> SupplierItems { get; set; }

    // For RadioButtonFor need below
    public Int32? CategorySelectedId { get; set; }
    public IEnumerable<Category> CategorieItems { get; set; }
}

<div class="form-group">
    @Html.LabelFor(model => model.Product.SupplierID, "SupplierID", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @*DropDownList is for deiplay purpose*@
        @*@Html.DropDownList("SupplierID", Model.SupplierItems, htmlAttributes: new { @class = "form-control" })*@

        @*DropDownListFor is for Edit purpose*@
        @Html.DropDownListFor(model => model.Product.SupplierID, Model.SupplierItems, htmlAttributes: new { @class = "form-control" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Product.CategoryID, "CategoryID", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @*RadioButton is for Display purpose*@
        @*@foreach (var Categorie in Model.CategorieItems)
            {
                @Html.RadioButton("CategoryID", Categorie.CategoryID, Model.CategorySelectedId == Categorie.CategoryID) @Categorie.CategoryName; @:&nbsp;&nbsp;&nbsp;
            }*@

        @*RadioButtonFor is for Edit purpose*@
        @foreach (var Categorie in Model.CategorieItems)
        {
            @Html.RadioButtonFor(model => model.CategorySelectedId, Categorie.CategoryID) @Categorie.CategoryName; @:&nbsp;&nbsp;&nbsp;
        }

    </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM