Parameter passing by GET with Asp.Net MVC?

1

I am creating a search screen and for this I created a Model with some attributes of type DateTime to do a search by date period. The problem is that when I pass these parameters to the method I always get nulls, but getting the values with Request works correctly.

My question is why does it happen and how do I fix it?

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")   
}
    
asked by anonymous 19.10.2017 / 19:04

1 answer

0

You do not need to pass each parameter individually, just pass the model, and within the get method it accesses it like this:

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

If you have any additional parameters passed by View in Html.BeginForm simply add it in the method parameters:

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

Tip: method name in capital letters.

Testa has solved, anything comments and I correct if applicable.

    
19.10.2017 / 19:58