【问题标题】:MVC 5 pass model from view to controller using actionlinkMVC 5 使用 actionlink 将模型从视图传递到控制器
【发布时间】:2014-03-20 15:30:40
【问题描述】:

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

这是我的模型:

 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 中获取值并将其传递给我的控制器

【问题讨论】:

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


    【解决方案1】:

    您不能使用@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">
    }
    

    【讨论】:

    • 这样做时如何访问模型?我的意思是在控制器中。
    • @inifus [HttpPost]public ActionResult UcakArama(Arama arama)MVC会将表单中的属性绑定到方法的参数上。
    • 完美!谢谢!
    【解决方案2】:

    你可以使用:

    @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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-06
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多