1

I am developing a sample in asp.net MVC 2.0. In My view (ProductDetails.aspx) I have 3 link buttons which are calling different action methods present in the same controller (ProductController). When I am clicking on the search link the appropriate action method is called but when I am taking data from Form Collection using string pid=Request.Form["tbPid"] I am getting a null value into pid. Help me in rectifying the issue.

Note: I dont have code for AddProduct link but I think there will be no issues.

Code for View:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MVCProductDetailsDemo.Models.ProductModel>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ProductDetails</title>
</head>
<body> 
    <div>
       <% using(Html.BeginForm()){ %>
        pid:<%=Html.TextBox("tbPid") %>
        <%=Html.ActionLink("Search Product", "SearchProducts", "Product")%><br />
        <%if (Model != null)
          { %>
        pname:<%=Html.TextBox("tbPname", Model.PName)%><br />
        Minqty:<%=Html.TextBox("tbMinQty",Model.MinOrdQty) %><br />
        Maxqty:<%=Html.TextBox("tbMaxQty",Model.MaxOrdQty) %><br />
        Up:<%=Html.TextBox("tbUP",Model.UnitPrice) %><br />
        <%} %>
        <%else { %>
              Pname:<%=Html.TextBox("tbPname") %>
              MinQty:<%= Html.TextBox("tbMinQty")%>
              MaxQty:<%=Html.TextBox("tbMaxQty")%>
              UP:<%=Html.TextBox("tbUP") %>
          <%} %>
        Manifacturer:<%=Html.DropDownList("manifacturers") %><br />




        <%=Html.ActionLink("Add Product","AddProduct","Product") %>
        <%=Html.ActionLink("Upd Product","UpdateProduct","Product") %>
        <%=Html.ActionLink("Del Product","DeleteProduct","Product") %>
       <%} %>
    </div>
</body>
</html>

**Code for Controller:**
 namespace MVCProductDetailsDemo.Controllers
{
    public class ProductController : Controller
    {
        //Normal Methods and ActionMethods.
        //ActionMethods are the methods which will usually call the views.
        public ActionResult DisplayProduct()
        {
            List<string> lstManifacturers=GetManifacturers();
            ViewData["manifacturers"] = new SelectList(lstManifacturers);
            return View("ProductDetails");
        }
        public List<string> GetManifacturers()
        {
            Manifacturers manifacturer = new Manifacturers();
            return manifacturer.GetManifacturers();
        }
        public ActionResult SearchProducts()
        {
            string pid=Request.Form["tbPid"];
            Products products = new Products();
            ProductModel pModel=products.GetProductDetails(Convert.ToInt32(pid));
            List<string> lstManifacturers = GetManifacturers();
            ViewData["manifacturers"] = new SelectList(lstManifacturers);
            return View("ProductDetails", pModel);
        }
        public ActionResult UpdateProduct()
        {
            string pid = Request.Form["tbPid"];
            string pname = Request.Form["tbPname"];
            string minqty = Request.Form["tbMinQty"];
            string maxqty = Request.Form["tbMaxQty"];
            string up = Request.Form["tbUP"];
            Products products = new Products();
            ProductModel pModel = new ProductModel();
            pModel.Mid = 1;
            pModel.PName = pname;
            pModel.MinOrdQty = Convert.ToInt32(minqty);
            pModel.MaxOrdQty = Convert.ToInt32(maxqty);
            pModel.Pid = Convert.ToInt32(pid);
            pModel.UnitPrice = Convert.ToInt32(up);
            bool isRecordUpdated = products.UpdateProducts(pModel);
            List<string> lstManifacturers = GetManifacturers();
            ViewData["manifacturers"] = new SelectList(lstManifacturers);
            return View("ProductDetails", pModel);
        }
        public ActionResult DeleteProduct()
        {
            string pid = Request.Form["tbPid"];
            Products products = new Products();
            bool isRecordDeleted = products.DeleteProduct(Convert.ToInt32(pid));
            List<string> lstManifacturers = GetManifacturers();
            ViewData["manifacturers"] = new SelectList(lstManifacturers);
            return View("ProductDetails");
        }
    }
}

1 Answer 1

1

Html.ActionLink produces anchor (a) tag that, when clicked, simply points the browser to a different address. What you need is a submit button that causes form data to be posted to the server. The url where form data is submitted can be controlled by the parameters of Html.BeginForm - during view rendering, or by javascript on the client as the value of action attribute of the form tag. If you don't specify Html.BeginForm paremeters (as in your code), the form will be posted to controller action with the same name as the one that produced the page. You need to create an overloaded action method (with parameter FormCollection collection, for example) and mark it with [HttpPost] attribute.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick reply . Can you give me a sample .
thanks for the help. it is working i used <button ...> control in the view and i am taking the button data from the controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.