通过GET向控制器发送参数?

时间:2017-10-19 17:39:31

标签: asp.net-mvc

我试图在我的系统中创建搜索,为此我创建了一个具有一些DateTime属性的模型,以按日期进行搜索。问题是,当我提交控制器接收空参数时,我不知道为什么会发生这种情况,因为Request["param"]它没有null

我该如何解决这个问题?

模型

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() { }
}

控制器

[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;
        //params (always null)
        DateTime dtm = DateTime.Now.Date;                
        DateTime start = dtIni ??  new DateTime(dtm.Year, dtm.Month, 1);
        DateTime end = dtFim ?? start.AddMonths(1).AddDays(-1);
        Debug.WriteLine("Start: " + start);
        Debug.WriteLine("End: " + end);
        //Request (not null)
        String di = Request["dtIni"]; // not null
        String df = Request["dtFim"]; // not null
        model.dtIni = di == null ? DateControl.getFirstDayOfMonth() : DateControl.convertStringToDate(di);
        model.dtFim = df == null ? DateControl.getLastDayOfMonth() : DateControl.convertStringToDate(df);
        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
....
@using (Html.BeginForm("viewAllPropPropriedadeIM", "PropostaInvestimento", FormMethod.Get, new { Class = "form-inline", role = "form" }))
{              
    @Html.TextBoxFor(model => model.dtIni, new { Class = "form-control date", placeholder = "Data inicial", style = "z-index:0 !important;" })

    @Html.TextBoxFor(model => model.dtFim, new { Class = "form-control date", placeholder = "Data final", style = "z-index:0 !important;" })

    @Html.DropDownListFor(model => model.statusSelected, Model.status, new { Class = "form-control", placeholder = "Status", style = "z-index:0 !important;" })

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

0 个答案:

没有答案