简体   繁体   English

视图中的Mvc4显示模型属性

[英]Mvc4 display model property in View

I have developed one module and it works fine. 我已经开发了一个模块,它工作正常。 I have few textboxes and submit button. 我有几个文本框和提交按钮。 When submitting textboxes values i will compare those with db values and return result and display. 提交文本框值时,我将比较它们与db值,并返回结果并显示。 I have used paging also. 我也用过分页。 This is my action method. 这是我的动作方法。

   [HttpGet]
        public ActionResult Index(int? clientId, DateTime? dateofAction, int? doc_typeid, string employeeID,string citizenId,int? currentFilter, DateTime? filterdateTime,int? filterdocType,string filteredemployeeID,string filteredcitizenId,int? 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??DateTime.Now, docType, employeeID, citizenId);
            IPagedList<logDetails> pagedLog = logDetails.ToPagedList(pageNumber, pageSize);
            model = new logDetailsEnumeration()
            {
                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 View 这是视图

<div class="forms">
    @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>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>
        }

Above code works fine. 上面的代码工作正常。 I have some difficulties. 我有些困难 Inside action method i have model. 内部动作方法我有模型。 Inside model i have Count=logDetails.Count. 在模型内部,我有Count = logDetails.Count。 Count will be holding number of rows returned. Count将保存返回的行数。 When the first time page loads the count value will be 0 and when there is no corresponding matching data in database then also count value will be 0. I want to display message something like below. 当第一次加载页面时,计数值将为0,而当数据库中没有相应的匹配数据时,计数值也将为0。我想显示以下消息。

var records = @Html.Raw(Model.Count);
             if(records==0)
             {
                 fun_toastr_notify('success','No Records Found');
             }

It also works but it also pops up message when the page loads first time(count will be zero). 它也可以使用,但是在页面第一次加载时也会弹出消息(计数为零)。 May i know how can I avoid this? 我可以知道如何避免这种情况吗? Thanks in advance. 提前致谢。

have you tried something like that? 你尝试过类似的东西吗? (that code has been written on the fly, not tested) (该代码是即时编写的,未经测试)

<script type="javascript/text">
         document.ready(function(){
            @if (Model.logDetails.Count != 0)
            {
                fun_toastr_notify('success','No Records Found');
            }
         };
</script>

for the first time pass a viewbag.isfirsttime = true depending on search parameters or whatever you have to differentiate if it is first time. 第一次通过viewbag.isfirsttime = true取决于搜索参数,或者如果是第一次则需要区别。 than in code 比在代码中

<script>

    @{
        var count = !ViewBag.IsFirstTime ? Model.Count : -1;
     };
    var records = @count;
    if (records == 0) {
        fun_toastr_notify('success', 'No Records Found');
    }
</script>

This will work surely. 这一定可以工作。 Happy Coding ! 编码愉快!

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

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