Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Object reference null

Controller Code



Error occurs when calling the page in view at line-----

////
@Html.ValidationMessageFor(x => x.search_type)
///

What I have tried:

<pre>   [Authorize]
        [HttpGet]
        public ActionResult ReportModule(string ac)
        {
            DbAccess da = new DbAccess();
            DisplayLndRep dn = new DisplayLndRep();
            LoginViewModel uinfo = new LoginViewModel();
            uinfo = TempData["UserInfo"] as LoginViewModel;
            lnd_prgm min_details = new lnd_prgm();
            dn.PageSize = 10;
            dn.LoginDetails = da.FindLoginDetails(User.Identity.Name);
            dn.FullName = da.FindUserInfoAD(User.Identity.Name).FullName;
            min_details = da.FindMeetingDetails(ac);
            if (min_details != null)
            {
                dn.FeedbackDetails = min_details;
            }
            //else
            //{               
            //    min_details.prgm_id = null;
            //    dn.FeedbackDetails = min_details;
            //}                   
            dn.FeedbackDetailList2 = da.FindAllMeetings();          
            return View(dn);
        }



Razor code

@using System.Web.Helpers
@model LndFeedbackReport.ViewModels.DisplayLndRep
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@*<br />*@
<div class="row">
    <div class="panel panel-success" style="padding-top: 0px;">
        <div class="panel-heading">
            <span style="color:blue">Welcome</span>   <span style="color:black">User : </span><span style="color:red">@Model.FullName</span>      <span style="color:black">Salary Code : </span><span style="color:red">@Model.EmployeeId</span>      <span style="color:black">Designation : </span><span style="color:red">@Model.Designation</span>      <span style="color:black">Department : </span><span style="color:red"> @Model.DepartmentName </span>

        </div>
    </div>
</div>
<div>
    @if (TempData["notice"] != null)
    {
        <p class="alert alert-danger" id="successMessage">@TempData["notice"]</p>
    }
</div>
@if (@Model.FeedbackDetailList2.Count() > 0)
        {
    <div class="row">
        @*@using (Ajax.BeginForm("SearchItems", "Home", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "result" }))*@
        @using (Html.BeginForm("SearchRepItems", "Home", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form", enctype = "multipart/form-data" }))
                {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(false)
            @Html.HiddenFor(x => x.Lnd_Program.prgm_id)
            <div class="form-horizontal">
                <div class="form-group">
                    <span class="control-label col-md-3" style="font-weight:bold">Search items</span>
                    <div class="col-md-9 col-lg-9">
                        <div class="col-md-4 col-lg-4">
                            @Html.TextBoxFor(x => x.search_text, new { maxlength = "100", @class = "form-control", autocomplete = "off" })
                        </div>
                        <div class="col-md-8 col-lg-8">
                            @Html.RadioButtonFor(x => x.search_type, "0", new { @onchange = "ToggleInfo();" }) Individual.
                            @Html.RadioButtonFor(x => x.search_type, "1", new { @onchange = "ToggleInfo();" }) Training Institute.
                            @*@Html.RadioButtonFor(x => x.search_type, "2", false) Minute ID*@
                            @Html.ValidationMessageFor(x => x.search_type)
                        </div>
                    </div>
                </div>
                <div class="row col-md-12 pull-left">
                    <div class="form-group">
                        <div class="col-md-offset-5 col-md-12">

                            <button type="submit" id="NwSave" class="btn btn-info">class="glyphicon glyphicon-search">Search</button>

                                <a class="btn btn-default" href="@Url.Action("Header", "Home", new { ac = @Model.Lnd_Program.prgm_id})">Reset</a>
                        </div>
                    </div>
                </div>
            </div>
                }
        
    </div>
        }
    <hr />
@*</div>*@
<hr />
<br />

<div class="wrap">
    <div class="content">
        @Html.AntiForgeryToken()
        <div class="inner-content" style="border:none">
            <span class="text-primary" style="color:crimson; font-size:larger">^__b>List of Feedback Submitted</span>

        </div>
    </div>
</div>
<br />
        </div>
    </div>
</div>
Posted
Updated 9-May-19 8:00am

1 solution

In your code, is x null?
@Html.ValidationMessageFor(x => x.search_type)
It might be that the null object is being called for a property. Maybe check if there would be a validation message. Try this,
C#
@if (!ViewData.ModelState.IsValid) {
   // Your code here.
}

This is one of the ways in which you are capturing the validation message, only if it exists. Otherwise, you ignore that statement at all.
 
Share this answer
 
Comments
Member 14330973 10-May-19 0:34am    
will u please tell me which code and where(controller)???
Afzaal Ahmad Zeeshan 10-May-19 5:43am    
You can use the wrapper condition block I provided around the code that is causing this issue. I am not talking about controllers, since this is a Razor view, use it in the same view page.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900