2014-08-28 46 views
1

以下代码的问题是当我点击“提交”按钮时,我希望它可以将我从查看页面带回控制器。我在控制器方法[HttpPost] ProductEdit中设置了一个断点,它永远不会停在那里。MVC查看页面提交不回控制器

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

请帮我找出原因,并提供详细的代码来解决它。


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> 
+0

退房页面上的HTML并验证<形式行动=点到家庭/ productEdit,而且方法是后。 – 2014-08-28 15:53:22

+0

您没有明确指定表单应该提交到哪里('@using(Html.BeginForm())'),所以在这种情况下它会提交给调用方法'/ Home/Index'。 – 2014-08-28 15:59:52

+0

您能否为您的代码提供ProductEditViewModel的定义,并解释提交表单时会发生什么?它会把你带到不同的行动吗? – 2014-08-28 15:59:54

回答

0

问题(查看页面不回发到[HttpPost]控制器)以下行的代码引起的:

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

修正c颂歌是:


[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> 

相关问题