Pass parameter (a list object) from View (type List) to Controller. Asp.NET MVC

2

I have a View of type "List<SimpleUser>" and I want to pass to my controller the object that is in my foreach, however this is arriving null being done as follows:

View:

@{
    ViewBag.Title = "Dashboard";
}

<!-- search form -->
<form action="#" method="get" class="sidebar-form">
    <div class="input-group">
        <input type="text" name="q" class="form-control" placeholder="Buscar entre seus clientes/pacientes..." />
        <span class="input-group-btn">
            <button type='submit' name='seach' id='search-btn' class="btn btn-flat"><i class="fa fa-search"></i></button>            
        </span>
    </div>
</form>

@if (Model.Count() > 0)
{
    foreach (var sUser in Model)
    {
        <div>            
            @Html.ActionLink(@sUser.Name, "ClientDetails", "ProfessionalUserHasClient", new { simpleUser = sUser })
        </div>
    }
}
else
{
    <label>Você não tem clientes/pacientes vinculados.</label>
}

Controller:

public ActionResult ClientDetails(SimpleUser simpleUser)
{
    SimpleUser sUser = ViewBag.SimpleUser as SimpleUser;
    return View();
}

If you change to @Html.Action() , it passes nicely however it is loaded inside View , a part of the entire layout, which obviously should not be loaded, follows a print of what it would look like:

    
asked by anonymous 02.06.2014 / 07:51

2 answers

3

Do not pass%% of the entire object. Pass only the Object Id. Making the binding of an entire object is very laborious and has a good chance of not working.

Switch:

@Html.ActionLink(@sUser.Name, "ClientDetails", "ProfessionalUserHasClient", new { simpleUser = sUser })

By:

@Html.ActionLink(@sUser.Name, "ClientDetails", "ProfessionalUserHasClient", new { id = sUser.Id }, null)

Do not forget to ActionLink at the end, otherwise it will fall into null wrong overload .

You will have to load the data again. There's no way around it.

The ActionLink looks like this:

public ActionResult ClientDetails(int id)
{
    var sUser = context.SimpleUsers.SingleOrDefault(su => su.Id == id);
    return View(sUser);
}

I did not understand the part that you load the data and does not send them to View . I imagine that Action of next Model is loaded with View statements. Therefore:

return View(sUser);

Sends the uploaded data to Action .

    
02.06.2014 / 20:50
3

Explanation:

In a @Html.ActionLink is returned a a element that in the case would be <a href=""></a> , so this instruction does not work, returning a complex data in this case will not have effect, whereas in a @Html.Action an output is returned in HTML , and executed in the case of a Controller and Action , even with data of the complex type ( class, object ) or primitive types, having a rendering as said of a HTML , as in its questioning has such execution.

So, it's different things having other results. By default when you use % is passed in its @Html.ActionLink , an ID of the record (the Id for example, that identifies such a line), example:

1) @ Html.ActionLink

In the View:

@if (Model.Count() > 0)
{
    foreach (var sUser in Model)
    {
        <div>            
            @Html.ActionLink(@sUser.Name, "ClientDetails", "ProfessionalUserHasClient", new { Id = sUser.Id }, null)
        </div>
    }
}

Na Action:

public ActionResult ClientDetails(int? Id)
{
    //recupera o Id
    return View();
}

2) @ Html.Action

It is used to load in the case of PartialView and of great utility for this, that is to say, fragments of pages loaded in layout already exists, with functionalities for example, of caching to increase the performance, diminishing the traffic in the network. >

Na View

@Html.Action("Menu", "Home")

Na Action

public PartialViewResult Menu()
{
    return PartialView("Menu");
}
    
02.06.2014 / 15:09