MVC 5使用actionlink将模型从视图传递到控制器

时间:2014-03-20 15:30:40

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

我想将我视图中收集的数据传递给我的控制器作为模型。 (因为我的变量太多,我只是显示很少)

这是我的模特:

 public class Arama
    {
        public string nereden { get; set; }
        public int neredenTip { get; set; }

        public string nereye { get; set; }
        public int nereyeTip { get; set; }
}

这是我的控制器:

public ActionResult UcakArama(Arama arama)
        {
            return RedirectToAction("Ukn", "U", arama);
        }

这是我的观点:

 @model  kyWeb.Models.Arama
    @Styles.Render("~/Content/AramaEkran")
       <li class="dyas_li">
          <div id="nereden">
            <span class="dyas_ttl">3.Çocuk</span>
               <div class="smll2-select">
                   @Html.DropDownListFor(m => m.nereden, new SelectList(new int[] { 2, 3, 4}, 2), new { tabindex = "1", id = "yds" })

                </div>
             </div>
          </li>
<li class="dyas_li">
          <div id="nereye">
            <span class="dyas_ttl">3.Çocuk</span>
               <div class="smll2-select">
                   @Html.DropDownListFor(m => m.nereye, new SelectList(new int[] { 2, 3, 4}, 2), new { tabindex = "1", id = "yds" })

                </div>
             </div>
          </li>
               @Html.ActionLink("ARA", "ucakarama", new { arama = this.Model })

当我调试时,我发现该模型正在变为空。  我想从html获取值并将其传递给我的控制器

2 个答案:

答案 0 :(得分:3)

您无法使用@Html.ActionLink,因为这会产生GET请求。您需要POST对控制器操作进行操作,方法是将您的属性包含在以下格式中:

@using (Html.BeginForm())
{
//properties go here
<li class="dyas_li">
    <div id="nereden">
    <span class="dyas_ttl">3.Çocuk</span>
        <div class="smll2-select">
            @Html.DropDownListFor(m => m.nereden, new SelectList(new int[] { 2, 3, 4}, 2), new { tabindex = "1", id = "yds" })
        </div>
    </div>
</li>
//...etc
<input type="submit" value="Submit">
}

答案 1 :(得分:0)

您可以使用:

@Ajax.ActionLink("Show", 
                 "Show", 
                 null, 
                 new AjaxOptions { HttpMethod = "GET", 
                 InsertionMode = InsertionMode.Replace, 
                 UpdateTargetId = "dialog_window_id", 
                 OnComplete = "your_js_function();" })

@ Ajax.ActionLink需要jQuery AJAX Unobtrusive库。您可以通过nuget下载它:

Install-Package Microsoft.jQuery.Unobtrusive.Ajax

然后将此代码添加到您的视图中:

@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")

这是MSDN documentation for Ajax.ActionLink