Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8910813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:47:02+00:00 2026-06-15T03:47:02+00:00

I`m trying to pass value in a controller from a text box. i google

  • 0

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:(

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:47:04+00:00Added an answer on June 15, 2026 at 3:47 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pass value by using EL expression in get request parameter, but
Ok, so i am trying to get the value from one textfield and pass
I'm trying to pass a null value from a RenderAction to another view. But
I'm trying to pass a value from my database to the view in asp.net
I am trying to pass a value from C++ to TCL. As I cannot
I am trying to pass a value from php to javascript. I understand one
I am trying pass class as value in hashmap. I need to get the
I'm trying to pass an Int value pulled from a JSON String to reduce
I have been trying to pass a string value from one method into another.
I am trying to pass some values to another View from the Controller .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.