错误"无法找到资源"提交表单时

时间:2017-11-30 19:05:28

标签: c# asp.net asp.net-mvc asp.net-mvc-5

我正在尝试在ASP中提交一个简单的表单,并且在识别我的路线时遇到了问题。我来自php背景,所以这对我来说都是新的。

  1. 如何正确注册我的路线?
  2. 如何在控制器中获取表单值?
  3. 我的表格:

    @using (Html.BeginForm("Create", "Product" , FormMethod.Post))
    {
        <div class="form-group">
            <label>Customer:</label>
            <select name="cus_no" class="form-control">
                <option value="2000">Target</option>
                <option value="4000">Wal-Mart</option>
                <option value="4200">Amazon</option>
            </select>
        </div>
        <div class="form-group">
            <label>Item:</label>
            <select name="item_no" class="form-control">
                <option value="6000">SkyWinder</option>
                <option value="60001">Somerset</option>
            </select>
        </div>
        <div class="form-group">
            <label>Quantity:</label>
            <input type="text" name="qty" class="form-control"/>
        </div>
        <div class="form-group">
            <label>Unit Price:</label>
            <input type="text" name="unit_price" class="form-control"/>
        </div>
        <div class="form-group">
            <label>Start Date:</label>
            <input type="date" name="start_date" class="form-control"/>
        </div>
        <div class="form-group">
            <label>Customer Warehouse Code:</label>
            <input type="text" name="cus_warehouse_cd" class="form-control" placeholder="optional"/>
        </div>
        <div class="form-group">
    
            <input type="submit" name="submit" class="btn btn-primary"/>
        </div>
    }
    

    我的控制器现在什么都不做。

    public class ProductController : Controller
    {
        // POST: Product/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
    
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
    

    我的RouteConfig.cs

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "Product",
                url: "Product/Create/{id}",
                defaults: new { controller = "Product", action = "Create", id = UrlParameter.Optional }
            );
        }
    }
    

0 个答案:

没有答案