Pass parameter through GET with Asp.Net MVC?

Asked

Viewed 501 times

1

I am creating a search screen and for this I created a Model with some attributes of the type Datetime to do a search by date period. The problem is that when I pass these parameters to the method they always return me null, but taking the values with the Request works properly.

My question is why this happens and how to fix ?

Model

public class SearchPropostaPropriedadeModel{

    public IPagedList<ViewPropostasPropriedadeModel> lista          { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime dtIni                                           { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime dtFim                                           { get;set; }    

    //Status
    public IEnumerable<SelectListItem> status                       { get; set; }    
    public int statusSelected                                       { get; set; }

    public SearchPropostaPropriedadeModel() { }
}

Controller

[HttpGet]
        [PermissionFilter(RoleType.Administrador, RoleType.Gerente, RoleType.InvestidorMaster)]
        public ViewResult viewAllPropPropriedadeIM(int? page, DateTime? dtIni, DateTime? dtFim, int? statusSelected){
            IList<PropInvestPropriedadeIM> _lista = new List<PropInvestPropriedadeIM>();
            IList<ViewPropostasPropriedadeModel> _listaModel = new List<ViewPropostasPropriedadeModel>();

            SearchPropostaPropriedadeModel model = new SearchPropostaPropriedadeModel();

            try{
                int pageSize = 20;
                int pagina = page ?? 1;

                String di = Request["dtIni"];
                String df = Request["dtFim"];

                model.dtIni = di == null ? DateControl.getFirstDayOfMonth() : DateControl.convertStringToDate(di);
                model.dtFim = df == null ? DateControl.getLastDayOfMonth() : DateControl.convertStringToDate(df);

                Debug.WriteLine("DI: " + model.dtIni);
                Debug.WriteLine("DF: " + model.dtFim);

                model.status = getStatus();
                model.statusSelected = statusSelected ?? 1;               

                _lista = new PropInvestPropriedadeDAO().findAllByData(model.dtIni, model.dtFim, model.statusSelected);
                foreach (PropInvestPropriedadeIM p in _lista){
                    ViewPropostasPropriedadeModel vppm = new ViewPropostasPropriedadeModel();
                    vppm.id = p.id;
                    vppm.dtPedido = p.dtPedido;
                    vppm.investidorNome = p.investidorMaster.nome;
                    vppm.refAnuncio = "#" + String.Format("{0:000}", p.anuncio.id); 
                    vppm.valorInvestimento = p.anuncio.valorInvestimento;
                    vppm.idAnuncio = p.anuncio.id;
                    vppm.statusDesc = getDescStatus(p.status);
                    _listaModel.Add(vppm);
                }
                model.lista = _listaModel.ToPagedList(pagina, pageSize);
            } catch (Exception e){
                Debug.WriteLine("Erro viewAllPropPropriedadeIM PropostaInvestimentoController: " + e.Message);
            }
            return View(model);
        }

HTML

    @model SearchPropostaPropriedadeModel
    @using PagedList.Mvc

    @{
        ViewBag.Title = "viewAllPropPropriedadeIM";
        Layout = "~/Views/Shared/_LayoutAdministracao.cshtml";
    }

    <!--consulta-->
    <div class="panel panel-red">
        <div class="panel-body">

            @using (Html.BeginForm("viewAllPropPropriedadeIM", "PropostaInvestimento", FormMethod.Get, new { Class = "form-inline", role = "form" }))        {


                <div class="form-group">   
                    <div class="input-group">                    
                        @Html.TextBoxFor(model => model.dtIni, new{

                                                                   Class = "form-control date",
                                                                   placeholder = "Data inicial",
                                                                   style = "z-index:0 !important;"
                                                               })
                        <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">                    
                        @Html.TextBoxFor(model => model.dtFim, new{
                                                                   Class = "form-control date",
                                                                   placeholder = "Data final",
                                                                   style = "z-index:0 !important;"
                                                               })
                        <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">                    
                        @Html.DropDownListFor(model => model.statusSelected, Model.status, new{
                                                                                               Class = "form-control",
                                                                                               placeholder = "Status",
                                                                                               style = "z-index:0 !important;"
                                                                                           })
                        @*<input type="text" class="form-control money2" placeholder="Investimento máximo USD$" id="max" name="max" size="25" style="z-index:0 !important;" />*@
                    </div>
                </div>

                <button type="submit" class="btn btn-default glyphicon glyphicon-search"></button>
            }

        </div><!--/panel body-->
    </div><!--/panel-->
    <!--/consulta-->

<div class="panel panel-red center-block">
    <div class="panel-heading bg-red clearfix">
        <strong>Propostas de investimentos em propriedades</strong>
    </div>
    <div class="panel-body">
        <table class="table table-bordered table-responsive table-striped table-hover" id="tableView">
            <thead class="CabecalhoTabela">
                <tr>
                    <th>#ID</th>
                    <th>Data</th>
                    <th>Investidor</th>
                    <th>Ref.Anúncio</th>
                    <th>Investimento USD$</th>
                    <th>Status</th>                   
                    <th>Controles</th>
                </tr>
            </thead>
            <tbody class="conteudoTabela">
                @foreach (ViewPropostasPropriedadeModel m in Model.lista){
                    <tr>
                        <td class="text-right">@Html.DisplayFor(i => m.id)</td>
                        <td class="text-center">@Html.DisplayFor(i => m.dtPedido)</td>
                        <td>@Html.DisplayFor(i => m.investidorNome)</td>
                        <td class="text-center">@Html.DisplayFor(i => m.refAnuncio)</td>
                        <td class="text-right">@Html.DisplayFor(i => m.valorInvestimento)</td>
                        <td class="text-center">@Html.DisplayFor(i => m.statusDesc)</td>                       
                        <td>
                            @*@Html.ActionLink(" ", "edit", "Propriedade", new { id = EncodingParams.encode(Convert.ToString(@m.id)) }, new { Class = "glyphicon glyphicon-pencil", title = "editar" })*@                            
                            @Html.ActionLink(" ", "detailPesquisa", "Pesquisa", new { id = EncodingParams.encode(Convert.ToString(@m.id)) }, new { Class = "glyphicon glyphicon-eye-open ", title = "detalhar" })
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
    <div class="panel-footer">
        Pagina @Model.lista.PageNumber de @Model.lista.PageCount
        @Html.PagedListPager(Model.lista, page => Url.Action("viewAllPropPropriedadeIM", new { pagina = page, Model.dtIni, Model.dtFim, Model.statusSelected }))
    </div>
</div>

@section Scripts{
    @Scripts.Render("~/Scripts/PluginsJS/jquery.base64.js")
    @Scripts.Render("~/bundles/jqueryval")  
    @Scripts.Render("~/bundles/Mascaras")  
    @Scripts.Render("~/bundles/Datetimepicker")  
    @Scripts.Render("~/Scripts/PersistObjects/PropostasInvest/CfgDatePicker.js")   
}

1 answer

0

You don’t need to pass each parameter individually, just pass the model, and within the get method access it like this:

public ViewResult viewAllPropPropriedadeIM(SearchPropostaPropriedadeModel model){
   Debug.WriteLine(model.statusSelected);
}

If you have an additional parameter being passed by View on Html.BeginForm just add it to the method parameters:

public ViewResult viewAllPropPropriedadeIM(SearchPropostaPropriedadeModel model, string param){
   Debug.WriteLine(model.statusSelected);
   Debug.WriteLine(param);
}

Tip: method name in upper case.

Test if solved, anything comments and I correct if it is the case.

  • The problem is that I pass these parameters on @Html.PagedListPager also to make the pagination using Pagedlist and I do not know if with this gives to send the whole model. I edited the post with the part of Pagedlist.

  • Do you really explain what your view does out of kindness? I don’t get it right

  • Basically I make a query for a period of date and return the model with a List of type Ipagedlist to fill a Table, and in the header has the query

  • Look in the footer of HTML q you will see that you have a Html.PagedListPager Step tbm for the controller method the parameters for paging.

Browser other questions tagged

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