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

Asked

Viewed 7,771 times

2

I own a view like "List<SimpleUser>" and I want to pass by parameter to my controller the object that is in my foreach, but this 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();
}

Change to @Html.Action(), it passes perfectly but is carried inside the View, an entire part of the layout, which obviously should not be loaded, follows a print of how it would look:

inserir a descrição da imagem aqui

2 answers

3


Don’t go through the ActionLink the whole object. Pass only the Id of the object. Do the Binding of an entire object is very laborious and has a good chance of not working.

Change:

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

For:

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

Don’t forget the null in the end, or you’ll fall into overload wrong of ActionLink.

You will have to load the data again. There will be no way.

To Action gets like this:

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

I don’t understand the part where you upload the data and don’t send it to View. I imagine that Model next View be loaded with the instructions of Action. Therefore:

return View(sUser);

Send the uploaded data to View.

3

Explanation:

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

So it’s different things having other results. By default when using @Html.ActionLink is passed in your a, an identification of the record (Id for example, identifying such a line), example:

1) @Html.Actionlink

Na View:

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

In Action:

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

2) @Html.Action

It is used to load in the case Partialview and very useful for this, ie fragments of pages loaded in existing layout, with features for example caching to increase performance, decreasing traffic in the network.

Na View

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

In Action

public PartialViewResult Menu()
{
    return PartialView("Menu");
}
  • Using Actionlink Id is passing as null. But like A partial View I couldn’t make it work. Can you explain more the partial code? If it works already this great without appearing half the layout again hehe

  • I need it to be a link, to this clientdetails.

  • @Luiznegrini I made for you as I should be put in Actionlink a new {Id = sUser.Id} and put in Action ClientDetails(int? Id), has already been passed this so that you fit your item and work like a link as you had already reported!

  • your code didn’t work because it lacked a parameter in it.

  • @Luiznegrini you are very deceived, look there in the code and practically equals the answer you gave as correct!!! Believe it or not the answer is the same! but, all right!

  • 1

    null parameter is missing after routevalues

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.