@ html.TextBox()不是通过在控制器中的ActionLink [英] @html.TextBox() is not passing in the controller in actionlink
问题描述
我真的试图从一个文本框中控制器传递价值。我谷歌这个问题。但没有得到这对我的作品的任何合适的解决方案。
下面是我的控制器。
WebProductController
块引用>使用系统;
使用System.Collections.Generic;
使用System.Data这;
使用System.Data.Entity的;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;
使用DatabaseService_WebAPI.Models;命名空间DatabaseService_WebAPI.Controllers
{
公共类WebProductController:控制器
{
私人ProductContext DB =新ProductContext();
私人LocalDBContext LDB =新LocalDBContext();
私人ProductTypeContext PDB =新ProductTypeContext(); //
// GET:/ WebProduct / 公众的ActionResult指数()
{
变种临时=从TPRO在db.Products
其中,tpro.User == User.Identity.Name
选择TPRO;
返回查看(temp.ToList());
} 公众的ActionResult TypeT()
{
变种临时=从pdb.ProductTypes于TTpro
其中,ttpro.Type ==平板
选择于TTpro;
返回查看(temp.ToList());
} 公众的ActionResult TypeC()
{
在pdb.ProductTypes变种临时=从ctpro
其中,ctpro.Type ==胶囊
选择ctpro;
返回查看(temp.ToList());
} 公众的ActionResult类型()
{
变种临时=从pdb.ProductTypes stpro
其中,stpro.Type ==糖浆
选择stpro;
返回查看(temp.ToList());
} //
// GET:/ WebProduct /详细资料/ 5 公众的ActionResult详细信息(INT ID = 0)
{
产品产品= db.Products.Find(ID);
如果(产品== NULL)
{
返回HttpNotFound();
}
返回查看(产品);
} //
// GET:/ WebProduct /创建 公众的ActionResult的Create()
{
返回查看();
} //
// POST:/ WebProduct /创建 [HttpPost]
公众的ActionResult创建(产品产品)
{
如果(ModelState.IsValid)
{
的LocalDB tobj = ldb.LocalDBs.Single(S => s.User == User.Identity.Name);
product.city = tobj.City;
product.OrderDate = DateTime.Now.Date.ToShortDateString();
product.ShopName = tobj.ShopName;
product.User = tobj.User; db.Products.Add(产品);
db.SaveChanges();
返回RedirectToAction(指数,WebProduct);
} 返回查看(产品);
} [HttpPost]
公众的ActionResult添加(ProductType类,串数量)
{ 产品产品=新产品();
如果(type.Type ==平板电脑)
{ //字符串为了= type.Name ++ type.Quantity;
的LocalDB tobj = ldb.LocalDBs.Single(S => s.User == User.Identity.Name); product.city = tobj.City;
product.OrderDate = DateTime.Now.Date.ToShortDateString();
product.ShopName = tobj.ShopName;
product.User = tobj.User;
//product.OrderDetail =秩序; db.Products.Add(产品);
db.SaveChanges(); 返回RedirectToAction(TypeT,WebProduct);
}
否则,如果(type.Type ==糖浆)
{
//字符串为了= type.Name ++ type.Quantity;
的LocalDB tobj = ldb.LocalDBs.Single(S => s.User == User.Identity.Name);
product.city = tobj.City;
product.OrderDate = DateTime.Now.Date.ToShortDateString();
product.ShopName = tobj.ShopName;
product.User = tobj.User;
// product.OrderDetail =秩序; db.Products.Add(产品);
db.SaveChanges(); 返回RedirectToAction(类型,WebProduct);
}
其他
{ //字符串为了= type.Name ++ type.Quantity;
的LocalDB tobj = ldb.LocalDBs.Single(S => s.User == User.Identity.Name);
product.city = tobj.City;
product.OrderDate = DateTime.Now.Date.ToShortDateString();
product.ShopName = tobj.ShopName;
product.User = tobj.User;
// product.OrderDetail =秩序; db.Products.Add(产品);
db.SaveChanges(); 返回RedirectToAction(TypeC,WebProduct);
} 返回查看();
}
//
// GET:/ WebProduct /编辑/ 5 公众的ActionResult编辑(INT ID = 0)
{
产品产品= db.Products.Find(ID);
如果(产品== NULL)
{
返回HttpNotFound();
}
返回查看(产品);
} //
// POST:/ WebProduct /编辑/ 5 [HttpPost]
公众的ActionResult编辑(产品产品)
{
如果(ModelState.IsValid)
{
db.Entry(产品).STATE = EntityState.Modified;
db.SaveChanges();
返回RedirectToAction(指数,WebProduct);
}
返回查看(产品);
} //
// GET:/ WebProduct /删除/ 5 公众的ActionResult删除(INT ID = 0)
{
产品产品= db.Products.Find(ID);
如果(产品== NULL)
{
返回HttpNotFound();
}
返回查看(产品);
} //
// POST:/ WebProduct /删除/ 5 [HttpPost,ActionName(删除)]
公众的ActionResult DeleteConfirmed(INT ID)
{
产品产品= db.Products.Find(ID);
db.Products.Remove(产品);
db.SaveChanges();
返回RedirectToAction(指数,WebProduct);
} 保护覆盖无效的Dispose(BOOL处置)
{
db.Dispose();
base.Dispose(处置);
}
}
}TypeT.cshtml
@model IEnumerable的< DatabaseService_WebAPI.Models.ProductType>@ {
ViewBag.Title =片; <脚本类型=文/ JavaScript的> $(函数(){
$('#编辑')。点击(函数(){
变种名称= $('#数量)VAL()。
this.href = this.href +'/'+ EN codeURIComponent(名);
});
});< / SCRIPT>
}< H2>及片剂LT; / H>
@ * @使用(Html.BeginForm(添加,WebProductController,FormMethod.Post)){
@ Html.ValidationSummary(真)
* @
<表>
&所述; TR>
<第i个
@ Html.DisplayNameFor(型号=> model.Name)
< /第i
<第i个
@ Html.DisplayNameFor(型号=> model.Price)
< /第i
<第i个
@ Html.DisplayNameFor(型号=> model.Batch)
< /第i
<第i个
@ Html.DisplayNameFor(型号=> model.Expiry)
< /第i
<第i个
@ Html.DisplayNameFor(型号=> model.Quantity)
< /第i
@ *下;第i
@ Html.DisplayNameFor(型号=> model.Type)
< /第i * @
百分位>< /第i
< / TR>@foreach(以型号VAR项){
&所述; TR>
&所述; TD>
@ Html.DisplayFor(modelItem => item.Name)
< / TD>
&所述; TD>
@ Html.DisplayFor(modelItem => item.Price)
< / TD>
&所述; TD>
@ Html.DisplayFor(modelItem => item.Batch)
< / TD>
&所述; TD>
@ Html.DisplayFor(modelItem => item.Expiry)
< / TD>
&所述; TD>
@ Html.TextBox(量)
< / TD>
@ Html.Hidden(idText,item.Id)
&所述; TD>
@ Html.ActionLink(添加,添加,新{ID = item.Id,名称= item.Name,键入=项目。形式},NULL)
@ * @ Html.ActionLink(添加,添加,空,新的{ID =编辑})* @
@ *<输入类型=提交值=添加/> * @
< / TD>
< / TR>
}< /表>
@ *} * @
< DIV>
@ Html.ActionLink(返回目录,创建)
< / DIV>
在我的控制器我真的调用add()方法。在行动导致它传递价值观的控制器,但没有通过文本框的值。当我尝试用
块引用>@using(Html.BeginForm(添加,WebProductController,FormMethod.Post))
然后形式没有按
T承认我的方法,当我使用按钮,在我的形式发送数据。
中号停留在了这个问题。但无解:(
我解决方案您控制器应该是这样的:
公众的ActionResult添加(IList的< ShoppingItemModel>项目){
的foreach(在项目ShopingItemModel项){
如果(item.Id!= NULL){
ShopingItem shopingItem = service.GetById(item.Id);
...添加项目到任何
}
}
}您ShoppingItemModel:
公共类ShoppingItemModel {
[需要]
公共标识{获取;设置;} [需要]
公众金额{获取;集;} ...
}您查看(我跳过Java脚本):
@model的IList< ShoppingItemModel> < H2>及片剂LT; / H>
@using(Html.BeginForm(添加,WebProductController,FormMethod.Post)){
@ Html.ValidationSummary(真) <表>
&所述; TR>
<第i个
名称
< /第i
<第i个
价钱
< /第i
...
<第i度< /第i
< / TR>@for(INT指数;指数< Model.Count;指数++){ &所述; TR>
&所述; TD>
@ Html.HiddenFor(型号=>模型[指数] .ID)
@ Html.DisplayFor(型号=>模型[指数] .Name点)
< / TD>
&所述; TD>
@ Html.DisplayFor(型号=>模型[指数]。价格)
< / TD>
&所述; TD>
@ Html.DisplayFor(型号=>模型[指数] .Batch)
< / TD>
...
&所述; TD>
@ Html.TextBoxFor(型号=>模型[指数] .Quantity)
< / TD>
< / TR>
}
< /表>
<输入类型=提交值=添加/>
}这是不完整的解决方案。只是一个提示。
托比I`m trying to pass value in a controller from a text box. i google this problem. but not get any suitable solution which works for me. below is my controller. WebProductController
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Web; using System.Web.Mvc; using DatabaseService_WebAPI.Models; namespace DatabaseService_WebAPI.Controllers { public class WebProductController : Controller { private ProductContext db = new ProductContext(); private LocalDBContext ldb = new LocalDBContext(); private ProductTypeContext pdb = new ProductTypeContext(); // // GET: /WebProduct/ public ActionResult Index() { var temp = from tpro in db.Products where tpro.User==User.Identity.Name select tpro; return View(temp.ToList()); } public ActionResult TypeT() { var temp = from ttpro in pdb.ProductTypes where ttpro.Type == "Tablet" select ttpro; return View(temp.ToList()); } public ActionResult TypeC() { var temp = from ctpro in pdb.ProductTypes where ctpro.Type == "Capsule" select ctpro; return View(temp.ToList()); } public ActionResult TypeS() { var temp = from stpro in pdb.ProductTypes where stpro.Type == "Syrup" select stpro; return View(temp.ToList()); } // // GET: /WebProduct/Details/5 public ActionResult Details(int id = 0) { Product product = db.Products.Find(id); if (product == null) { return HttpNotFound(); } return View(product); } // // GET: /WebProduct/Create public ActionResult Create() { return View(); } // // POST: /WebProduct/Create [HttpPost] public ActionResult Create(Product product) { if (ModelState.IsValid) { LocalDB tobj = ldb.LocalDBs.Single(s => s.User == User.Identity.Name); product.city = tobj.City; product.OrderDate = DateTime.Now.Date.ToShortDateString(); product.ShopName = tobj.ShopName; product.User = tobj.User; db.Products.Add(product); db.SaveChanges(); return RedirectToAction("Index", "WebProduct"); } return View(product); } [HttpPost] public ActionResult Add(ProductType type, string quantity) { Product product = new Product(); if (type.Type=="Tablet") { //string order = type.Name + " " + type.Quantity; LocalDB tobj = ldb.LocalDBs.Single(s => s.User == User.Identity.Name); product.city = tobj.City; product.OrderDate = DateTime.Now.Date.ToShortDateString(); product.ShopName = tobj.ShopName; product.User = tobj.User; //product.OrderDetail = order; db.Products.Add(product); db.SaveChanges(); return RedirectToAction("TypeT", "WebProduct"); } else if (type.Type == "Syrup") { //string order = type.Name + " " + type.Quantity; LocalDB tobj = ldb.LocalDBs.Single(s => s.User == User.Identity.Name); product.city = tobj.City; product.OrderDate = DateTime.Now.Date.ToShortDateString(); product.ShopName = tobj.ShopName; product.User = tobj.User; // product.OrderDetail = order; db.Products.Add(product); db.SaveChanges(); return RedirectToAction("TypeS", "WebProduct"); } else { // string order = type.Name + " " + type.Quantity; LocalDB tobj = ldb.LocalDBs.Single(s => s.User == User.Identity.Name); product.city = tobj.City; product.OrderDate = DateTime.Now.Date.ToShortDateString(); product.ShopName = tobj.ShopName; product.User = tobj.User; // product.OrderDetail = order; db.Products.Add(product); db.SaveChanges(); return RedirectToAction("TypeC", "WebProduct"); } return View(); } // // GET: /WebProduct/Edit/5 public ActionResult Edit(int id = 0) { Product product = db.Products.Find(id); if (product == null) { return HttpNotFound(); } return View(product); } // // POST: /WebProduct/Edit/5 [HttpPost] public ActionResult Edit(Product product) { if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index", "WebProduct"); } return View(product); } // // GET: /WebProduct/Delete/5 public ActionResult Delete(int id = 0) { Product product = db.Products.Find(id); if (product == null) { return HttpNotFound(); } return View(product); } // // POST: /WebProduct/Delete/5 [HttpPost, ActionName("Delete")] public ActionResult DeleteConfirmed(int id) { Product product = db.Products.Find(id); db.Products.Remove(product); db.SaveChanges(); return RedirectToAction("Index", "WebProduct"); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } } }
TypeT.cshtml
@model IEnumerable<DatabaseService_WebAPI.Models.ProductType> @{ ViewBag.Title = "Tablets"; <script type="text/javascript"> $(function () { $('#edit').click(function() { var name = $('#quantity').val(); this.href = this.href + '/' + encodeURIComponent(name); }); }); </script> } <h2>Tablets</h2> @*@using (Html.BeginForm("Add", "WebProductController",FormMethod.Post)) { @Html.ValidationSummary(true) *@ <table> <tr> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> @Html.DisplayNameFor(model => model.Price) </th> <th> @Html.DisplayNameFor(model => model.Batch) </th> <th> @Html.DisplayNameFor(model => model.Expiry) </th> <th> @Html.DisplayNameFor(model => model.Quantity) </th> @* <th> @Html.DisplayNameFor(model => model.Type) </th>*@ <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.DisplayFor(modelItem => item.Price) </td> <td> @Html.DisplayFor(modelItem => item.Batch) </td> <td> @Html.DisplayFor(modelItem => item.Expiry) </td> <td> @Html.TextBox("quantity") </td> @Html.Hidden("idText", item.Id) <td> @Html.ActionLink("Add", "Add", new { id = item.Id, name=item.Name, type=item.Type }, null) @*@Html.ActionLink("Add", "Add", null, new { id = "edit" })*@ @*<input type="submit" value="Add" />*@ </td> </tr> } </table> @*}*@ <div> @Html.ActionLink("Back to List", "Create") </div>
In my controller i`m calling Add() method. in action result it is passing values in the controller but not passing the textbox value. when i try to use
@using (Html.BeginForm("Add", "WebProductController",FormMethod.Post))
Then the form doesn
t recognize my method when i use button to sending data in my form. i
m stuck in this problem. but no solution:(解决方案Your Controller should look like:
public ActionResult Add(IList<ShoppingItemModel> items){ foreach(ShopingItemModel item in items){ if (item.Id != null){ ShopingItem shopingItem = service.GetById(item.Id); ... Add Item to whatever } } }
Your ShoppingItemModel:
public class ShoppingItemModel{ [Required] public Id{get;set;} [Required] public Amount{get;set;} ... }
Your View (I Skipped Java Script):
@model IList<ShoppingItemModel> <h2>Tablets</h2> @using (Html.BeginForm("Add", "WebProductController",FormMethod.Post)) { @Html.ValidationSummary(true) <table> <tr> <th> Name </th> <th> Price </th> ... <th>Amount</th> </tr> @for(int index; index < Model.Count; index++) { <tr> <td> @Html.HiddenFor(model => model[index].Id) @Html.DisplayFor(model => model[index].Name) </td> <td> @Html.DisplayFor(model => model[index].Price) </td> <td> @Html.DisplayFor(model => model[index].Batch) </td> ... <td> @Html.TextBoxFor(model => model[index].Quantity) </td> </tr> } </table> <input type="submit" value="Add" /> }
That is not the complete solution. Just a hint. Tobi
这篇关于@ html.TextBox()不是通过在控制器中的ActionLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!