我的代码如下。我希望当我点击" Save"按钮视图页面可以让我回到控制器[HttpPost]" ProductEdit",但它没有。任何人都可以帮助我将非常感激。请检查下面的视图和控制编码。
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
{
[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()
{
var northwind = new NorthwindEntities();
var vm = new ProductEditViewModel();
UpdateModel<ProductEditViewModel>(vm);
return View();
}
}
}
@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">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>
答案 0 :(得分:0)
正确使用Html.BeginForm可能会有所帮助:
Html.BeginForm(“ActionName”,“ControllerName”,Method)
示例:
@using (Html.BeginForm("ProductEdit", "Home", FormMethod.Post))
答案 1 :(得分:0)
问题(不从视图回发到[HttpPost]控制器)是由以下代码行引起的
Value = SqlFunctions.StringConvert((double)sup.SupplierID),
所以更正码是:
[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; @:
}*@
@*RadioButtonFor is for Edit purpose*@
@foreach (var Categorie in Model.CategorieItems)
{
@Html.RadioButtonFor(model => model.CategorySelectedId, Categorie.CategoryID) @Categorie.CategoryName; @:
}
</div>
</div>