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 doesnt recognize my method when i use button to sending data in my form.
i
m stuck in this problem. but no solution:(