多个提交按钮在控制器中获取空值?

时间:2013-07-30 11:11:33

标签: c# asp.net-mvc-4 razor submit

我正在使用mvc4并从http://api.feedzilla.com/v1/categories.json

获取JSON

我的模型有以下代码

public class catagorygroup
    {

        public List<CatagoryModel> catagoryModel { get; set; } 

    }

    public class CatagoryModel
    {

        public int category_id { get; set; }
        public string english_category_name { get; set; }
    }

我的观点是这样的

        @for (int i = 0; i < Model.catagoryModel.Count; i++)
        {
            using (Html.BeginForm("News", "Catagory"))
            {

            <li> <input type="submit"   name="w8-red" class="w8-button red"  value= @Model.catagoryModel[i].english_category_name   /> </li>
        @Html.HiddenFor(model => model.catagoryModel[i].category_id);
        @Html.HiddenFor(model => model.catagoryModel[i].english_category_name); 

        <br/><br/>
            }
            }     

我的观点是这样的

enter image description here

如果我点击第一个按钮体育按钮,我会得到“姓名,身份证”这样的值

enter image description here

但如果我点击第一个按钮以外的任何按钮即可获得此类[Null值] enter image description here

我的代码出了什么问题

2 个答案:

答案 0 :(得分:1)

看起来它可能更适合作为锚标记。

@Html.ActionLink(model.catagoryModel[i].english_category_name,
                 "News",
                 "Catagory",
                 new { @Model.catagoryModel[i].english_category_name,
                       @Model.catagoryModel[i].category_id },
                 new { @class = "w8-button red" });

请参阅:LinkExtensions.ActionLink

样品:

//HomeController.cs

public class HomeController : Controller
{
     public ActionResult Index()
     {
         return View();
     }

    public ActionResult DoWork(SomeDTO dto)
    {
        return View("Index");
    }
}

public class SomeDTO
{
    public int SomeId { get; set; }
    public string SomeData { get; set; }
}

//Index.cshtml

@Html.ActionLink("Home", "DoWork", new { SomeId = 1, SomeData = "World" })

//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 }
        );
    }
}

答案 1 :(得分:0)

看看这个,它可能有效

@Html.ActionLink(model.catagoryModel[i].english_category_name,
             "News",
             new { @Model.catagoryModel[i].english_category_name,
                   @Model.catagoryModel[i].category_id },
             new { @class = "w8-button red" });