将值从页面传递到Html.ActionLink

时间:2014-05-18 18:06:56

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

我的问题是:

我有两个字段,当我调用我的@ 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();
        }

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

2 个答案:

答案 0 :(得分:1)

您是否尝试过将controllerName添加到actionLink?

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

答案 1 :(得分:0)

不使用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"/>

}}

说到这一点,通常你应该将这些值发送回服务器,然后重定向到一个新的ActionMethod(因此,Post,Redirect,Get的首字母缩略词PRG)。