简体   繁体   English

当模型不返回任何数据时,如何在MVC4的视图中显示消息?

[英]How to display messages in view in MVC4 when model does not return any data?

Hi I am developing application where i have 5 textboxes and search button. 嗨,我正在开发应用程序,我有5个文本框和搜索按钮。 When i enter data into textbox and click on search, corresponding matched data of database will be displayed in below table. 当我在文本框中输入数据并单击搜索时,下表中将显示数据库的匹配数据。 I have implemented paging and it works fine. 我已经实现了分页,并且工作正常。 Below is the code. 下面是代码。

 [HttpGet]
        public ActionResult Index(int? clientId, string dateofAction, int? doc_typeid, string employeeID,string citizenId,int? currentFilter, string filterdateTime,int? filterdocType,string filteredemployeeID,string filteredcitizenId,int? page)
        {
            ViewBag.curpageNumber = page;

            logDetailsEnumeration model = new logDetailsEnumeration();
            DB_KYC3Entities db = new DB_KYC3Entities();
            var docTypes = from c in db.tm_doc_type select c;
            if (clientId != null)
            {
                page = 1;
            }
            else
            {
                clientId = currentFilter;
            }
            if(dateofAction!=null)
            {
                page = 1;
            }
            else
            {
                dateofAction = filterdateTime;
            }
            if(doc_typeid != null)
            {
                page = 1;
            }
            else
            {
                doc_typeid = filterdocType;

            }
            if(employeeID!=null)
            {
                page = 1;
            }
            else
            {
                employeeID = filteredemployeeID;
            }
            if(citizenId!=null)
            {
                page = 1;
            }
            else
            {
                citizenId = filteredcitizenId;
            }

            ViewBag.CurrentFilter = clientId;
            ViewBag.filterdateTime = dateofAction;
            int pageSize = 8;
            int pageNumber = (page ?? 1);
            VerificationLogBAL obj = new VerificationLogBAL();
            int docType = obj.GetDocDetails(doc_typeid?? default(int));
            List<logDetails> logDetails = obj.getlogDetails(clientId?? default(int), dateofAction, docType, employeeID, citizenId);
            IPagedList<logDetails> pagedLog = logDetails.ToPagedList(pageNumber, pageSize);
            model = new logDetailsEnumeration()
            {
                pageNum= pageNumber,
                doc_typeid = doc_typeid,
                Count=logDetails.Count,
                employeeID = employeeID,
                citizenId= citizenId,
                logDetails = pagedLog,
                doctype_name=new SelectList(docTypes, "doc_typeid", "doctype_name")
            };
            return View("Index",model);
            }
        }

This is my view code. 这是我的查看代码。

@using (Html.BeginForm("Index", "VerificationLog", FormMethod.Get))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        <div class="message"></div>

        <div class="loginUsernamePassword">
            <i class="fa fa-user"></i>
            <table width="100%" border="0" cellspacing="0" cellpadding="0" class="dataTable tableHover">
                <tr>
                    <th width="8%" scope="col">Client ID</th>
                    <th width="20%" scope="col">
                        <div class="form-box form-box-default">
                            @Html.TextBoxFor(x=>x.clientId, ViewBag.CurrentFilter as string, new { @id = "clientId", @placeholder = "Client ID", @class = "form-control", @maxlength = 20 })

                        </div>
                    </th>
                   <th width="10%" scope="col">Date Of Action</th>
                    <th width="20%" scope="col">
                        <div class="form-box form-box-default">
                         @Html.TextBoxFor(x=>x.dateofAction, ViewBag.filterdateTime as string,  new { @id = "dateofAction", @placeholder = "Date Of Action", @class = "txtBox form-control calender validate[required]" })
                            @*@Html.TextBox("dateofAction", ViewBag.filterdateTime as string,  new { @id = "dateofAction", @placeholder = "Date Of Action", @class = "txtBox form-control calender validate[required]" })*@


                        </div>
                    </th>
                    <th width="15%" scope="col">Type Of Document</th>
                    <th width="17%" scope="col">
                        <div class="form-box form-box-default">
                            @*@Html.TextBox("typeofDocument", ViewBag.filterdateTime as string, new { @id = "typeofDocument", @placeholder = "Type Of Document", @class = "form-control", @maxlength = 20 })*@
                            @Html.DropDownListFor(x=>x.doc_typeid,Model.doctype_name,"Select",new { @class = "form-control" })

                                                </div>
                    </th>
                </tr>
                <tr>
                    <th width="15%" scope="col">Employee ID</th>
                    <th width="17%" scope="col">
                        <div class="form-box form-box-default">
                            @Html.TextBoxFor(x=>x.employeeID, Model.employeeID, new { @id = "employeeID", @placeholder = "Employee ID", @class = "form-control", @maxlength = 20 })

                        </div>
                    </th>
                    <th width="15%" scope="col">Citizen ID</th>
                    <th width="17%" scope="col">
                        <div class="form-box form-box-default">
                            @Html.TextBoxFor(x=>x.citizenId, Model.citizenId, new { @id = "citizenId", @placeholder = "Citizen ID", @class = "form-control", @maxlength = 20 })

                        </div>
                    </th>
                    <th width="10%" scope="col" colspan="2">
                        <input type="submit" value="Search" class="btn btn-primary btn-cons search" />
                </tr>
            </table>

            </div>
            }

        </div>

        @if (Model != null && Model.logDetails.Count != 0)
        {
            <br />
            <h2>Verification Log</h2>
            <br />
            <div id="GridDetails">
                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="dataTable tableHover">
                    <tr>
                        @*<th>@Html.ActionLink("Label", "Index", new { sortOrder = ViewBag.LabelSortParm, currentFilter = ViewBag.CurrentFilter, filterdateTime = ViewBag.filterdateTime, filterdocType = Model.doc_typeid, filteredemployeeID = Model.employeeID, filteredcitizenId = Model.citizenId, Page = ViewBag.curpageNumber })</th>*@
                        <th>Label</th>
                        <th>Value</th>
                        <th>UpdatedOn</th>
                        <th>UpdatedBy</th>
                        <th>UpdatedStatus</th>
                        <th>RejectComment</th>
                    </tr>
                    @foreach (var group in Model.logDetails)
                    {
                        <tr>
                            <td>@group.contentLabel</td>
                            <td>@group.contentValue</td>
                            <td>@group.updatedOn</td>
                            <td>@group.updatedBy</td>
                            <td>@group.updatedStatus</td>
                            <td>@group.rejectComment</td>
                        </tr>

                    }
                </table>

                @Html.PagedListPager(Model.logDetails, page => Url.Action("Index",
new { page, currentFilter = ViewBag.CurrentFilter, filterdateTime=ViewBag.filterdateTime, filterdocType= Model.doc_typeid, filteredemployeeID = Model.employeeID, filteredcitizenId = Model.citizenId }))
                Page @(Model.logDetails.PageCount < Model.logDetails.PageNumber ? 0 : Model.logDetails.PageNumber) of @Model.logDetails.PageCount
            </div>

I am having problem in displaying message No records found. 我在显示消息“找不到记录”时遇到问题。 If i try to put No records found by checking following property if(Model!=null) then When the first time page loads then also it display No records found. 如果我尝试通过检查以下属性if(Model!= null)来放置“未找到记录”,则当首次加载页面时,它还会显示“未找到记录”。 I want to display only after clicking on submit button and if there are no matching records found. 我只想在单击“提交”按钮并且没有找到匹配的记录后显示。 Is there any way I can implement solution in above scenario? 有什么办法可以在上述情况下实施解决方案? Thanks 谢谢

You could add a new property to the model that would only be set when the search has been completed. 您可以将新属性添加到模型中,只有在搜索完成后才能设置。 This would indicate if there were no records found and you could use logic in the view to display an appropriate message. 这将指示是否找不到记录,您可以在视图中使用逻辑来显示适当的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM