square
square

Reputation: 123

Use Html.BeginForm can not find action name in control (asp.net mvc)

This is my control

enter image description here

/ProductController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShoppingStore.Domain.Abstract;
using ShoppingStore.Domain.Entities;
using ShoppingStore.WebUI.Models;

namespace ShoppingStore.WebUI.Controllers
{
    public class ProductController : Controller
    {
        private IProductsRepository repository; 

        public int PageSize = 4;

        public ProductController(IProductsRepository productRepository)
        {
            this.repository = productRepository;
        }


        public ViewResult List(string category , int page=1)
        {
            ProductsListViewModel model = new ProductsListViewModel
            {
                // Products = repository.Products.OrderBy(p => p.ProductID).Skip((page - 1) * PageSize).Take(PageSize),
                Products = repository.Products.Where(p => category ==null || p.Category ==category).OrderBy(p => p.ProductID).Skip((page - 1) * PageSize).Take(PageSize),
                PageInfo = new PagingInfo {CurentPage = page,ItemPerPage = PageSize,TotalItems = category == null ? repository.Products.Count() : repository.Products.Where(e => e.Category == category).Count()},
                CurrentCategory = category
            };
     
            return View(model);
        }  
       [HttpPost]
        public ActionResult Save(List<string> hiddenList)
        {
            return View();
        } 

    }
}

enter image description here

This is View

/Views/Product/List.cshtml

@model ShoppingStore.WebUI.Models.ProductsListViewModel

@{
    ViewBag.Title = "Prodcuts";
 
}

<script src="~/Scripts/pop_window.js"></script>
<script src="~/Scripts/tool.js"></script>

@foreach (var p in Model.Products)
{
 @Html.Partial("ProductSummary",p)
}
<div>
    @Html.PageLinks(Model.PageInfo, x => Url.Action("List", new { page = x , category = Model.CurrentCategory}))
</div>
<div>
 
    <div id="result" style="display:none;"></div>

    @using (Html.BeginForm("Save", "Product", FormMethod.Post))
    {
        <div id="dialog" title="Basic dialog">
            <ol id="test"></ol>
            <div id="detail">
                <div>
                    <input class="ice-checkbox" type="checkbox" name="driink_d" id="BigICE" value="BigICE">cold/big/L $8<br>
                    <input class="ice-checkbox" type="checkbox" name="driink_d" id="MidHOT" value="MidHOT">hot/mid/M $7<br>
                    <input class="ice-checkbox" type="checkbox" name="driink_d" id="BigHOT" value="BigHOT">hot/big/L $7<br>
                </div>
                <div>
                    <input class="size-checkbox" type="checkbox" name="driink_d" id="NOICE" value="NOICE">no ice<br>
                    <input class="size-checkbox" type="checkbox" name="driink_d" id="NORL" value="NORL">normal<br>
                    <input class="size-checkbox" type="checkbox" name="driink_d" id="LESSICE" value="LESSICE">less ice<br>
                </div>
                <div>
                    <input class="sugar-checkbox" type="checkbox" name="driink_d" id="LESS" value="LESS">Less<br>
                    <input class="sugar-checkbox" type="checkbox" name="driink_d" id="HALF" value="HALF">half<br>
                    <input class="sugar-checkbox" type="checkbox" name="driink_d" id="FULL" value="FULL">full<br>
                    <input class="sugar-checkbox" type="checkbox" name="driink_d" id="NOS" value="NOS">no<br>
                </div>
            </div>

            <div>
                <div id='DIV2'><input type="button" onclick="incrementValue1()" value="ADD" /></div>
                <div id="DIV2"> <input type="text" id="number1" value="0" /></div>
                <div id="DIV2"><input type="button" onclick="decrementValue1()" value="minus" /></div>
            </div>


            <button id="checkvalue" onclick="checkValue()" type="submit">Check</button>

        </div>
 
    }

When my project starts running, List.cshtml work is fine .

It can read data from the database, and show information to browser.

But I have a form, want to send some values to ProductController.cs .

It can not work, even I put breakpoint in ProductController.cs .

use Html.BeginForm("Save", "Product", FormMethod.Post)

It can not find 'Save' action name in the ProductController.cs

Upvotes: 0

Views: 1647

Answers (2)

Robin Lin
Robin Lin

Reputation: 11

I have tested your code and it works fine on my machine. I noticed that you will trigger a Javascript function when submitting your form, this maybe where causes the problem. Press F12 in browse to turn on developer tools and check if there is any error shows in the console.

Or if it showed an error page, it would be very helpful if you could shows more details of the error to let us help you.

Besides, there are several parts you need to change to pass form values to controller successfully:

  1. Html attribute id is used to reference to the element itself so it should be unique. If there are multiple elements have the same id in the same page, only the first one will be found when you use id to access it in Javascript.

  2. Html attribute name is used to reference the value of elements when a form is submitted, and .NET MVC uses it to bind values with a controller's parameters. Make sure the name you gave matches the name of controller's parameters.

     <div id="DIV2"> <input type="text" name="number1" value="0" /></div>
    

    And your controller should look like this:

    [HttpPost]
    public ActionResult Save(List<string> driink_d, int number1)
    {
        // Do something here
    }
    

If it showed an error page, it would be very helpful to include details of the error.

Upvotes: 1

Nikunj Patel
Nikunj Patel

Reputation: 247

You should change your method signature with below :

[HttpPost]
 public ActionResult Save(ProductsListViewModel modelData)
        {
            return View();
        }

Now check and let me know !!

Upvotes: 2

Related Questions