Burak Gazi
Burak Gazi

Reputation: 595

MVC 5 pass model from view to controller using actionlink

I want to pass data collected in my view to be passed to my controller as model. (cause my variables are too much I am just showing few )

Here is my Model :

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

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

Here is my controller :

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

and here is my view :

 @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 })

When I debug , I see that the model is turning null. I want to get the values from html and pass it into my controller

Upvotes: 2

Views: 8543

Answers (2)

Ricardo Rodriguez
Ricardo Rodriguez

Reputation: 421

You can use:

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

@Ajax.ActionLink requires jQuery AJAX Unobtrusive library. You can download it via nuget:

Install-Package Microsoft.jQuery.Unobtrusive.Ajax

Then add this code to your View:

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

Here's the MSDN documentation for Ajax.ActionLink

Upvotes: 0

SOfanatic
SOfanatic

Reputation: 5573

You can't use the @Html.ActionLink because that produces a GET request. You need to POST to your controller action by enclosing your properties in a form:

@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">
}

Upvotes: 3

Related Questions