0

我正在尝试从文本框中传递控制器中的值。我谷歌这个问题。但没有得到任何适合我的解决方案。下面是我的控制器。 网络产品控制器

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);
        }
    }
}

类型T.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>

在我的控制器中,我正在调用 Add() 方法。在行动结果中,它在控制器中传递值但不传递文本框值。当我尝试使用

@using (Html.BeginForm("Add", "WebProductController",FormMethod.Post))

然后表格不会t recognize my method when i use button to sending data in my form. i陷入这个问题。但没有解决方案:(

4

1 回答 1

1

您的控制器应如下所示:

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

您的 ShoppingItemModel:

public class ShoppingItemModel{
     [Required]
     public Id{get;set;}

     [Required]
     public Amount{get;set;}

     ...
}

您的观点(我跳过了 Java 脚本):

 @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" />
}

这不是完整的解决方案。只是一个提示。托比

于 2012-11-28T14:52:19.690 回答