【问题标题】:Passing value from page to Html.ActionLink将值从页面传递到 Html.ActionLink
【发布时间】:2014-05-18 18:06:55
【问题描述】:

我的问题是:

我有两个字段,当我调用 @Html.Actionlink 方法时,它会为这两个参数发送一个空值。

这是我的页面代码:

            <div id="new-skill" class="row">
                <label for="Description">Descreva brevemente a sua habilidade:</label>
                @Html.TextBoxFor(model => model.skill.Description, new { @class = "form-control" })

                <label for="Name">Em qual categoria ela está?</label>
                @Html.TextBoxFor(model => model.skill.Category.Name, new { @class = "form-control" })

                <div class="text-center margin-top15">

                    @Html.ActionLink("Adicionar nova habilidade", "InsertNewSkill", new
                                                                                   {
                                                                                       professionalId = ViewBag.professionalId,
                                                                                       skillDescription = "Test Text",
                                                                                       categoryName = Model.skill.Category.Name
                                                                                       }, new 
                                                                                       { 
                                                                                           @class = ""
                                                                                       })
                    </div>

                </div>

这是我的 InsertNewSkill 方法:

        public ActionResult InsertNewSkill(int professionalId, string skillDescription, string categoryName)
        {
            initBusinessObjects();

            var professional = professionalBusiness.GetById(professionalId);
            var newSkill = new SkillModel { Description = skillDescription, Category = new SkillCategoryModel { Name = categoryName } };

            skillBusiness.Insert(newSkill);
            professional.Skills.Add(newSkill);

            professionalBusiness.Update(professional);

            return View();
        }

我必须做些什么来实现这一点(发送文本框值)?

【问题讨论】:

    标签: c# asp.net-mvc html-helper html.actionlink


    【解决方案1】:

    您是否尝试将控制器名称添加到您的 actionLink?

     @Html.ActionLink("Adicionar nova habilidade", "InsertNewSkill","CONTROLLER_NAME", new
                 {
                  professionalId = ViewBag.professionalId,
                  skillDescription = "Test Text",
                  categoryName = Model.skill.Category.Name
                   }, new 
                   { 
                   @class = ""
                   })
    

    【讨论】:

      【解决方案2】:

      不使用 jQuery / javascript,您应该使用表单将这些值返回到服务器。

      @{using(Html.BeginForm("InsertNewSkill", "ControllerName", FormMethod.Get)){
         <div id="new-skill" class="row">
         <label for="Description">Descreva brevemente a sua habilidade:</label>
         @Html.TextBoxFor(model => model.skill.Description, new { @class = "form-control" })
      
         <label for="Name">Em qual categoria ela está?</label>
         @Html.TextBoxFor(model => model.skill.Category.Name, new { @class = "form-control" })
      
         @Html.Hidden("professionalId", ViewBag.professionalId)
      
         <div class="text-center margin-top15">
               <input type="submit" value="Adicionar nova habilidade"/>
      
      }}
      

      话虽如此,通常您应该将这些值 POST 回服务器,然后重定向到新的 ActionMethod(因此,PRG 的首字母缩写词是 Post、Redirect、Get)。

      【讨论】:

      • 但是我在同一页面中有另一个按钮可以发布另一个信息:/ 你有使用 jquery 的例子吗?
      • @guisantogui - 您可以在一个页面上拥有多个表单...看起来这些字段并未在您的其他表单中捕获。如果是,那你为什么不同时处理所有这些信息(创造技能和其他任何事情)?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多