0

Model: ProjectModel declared project id in model.

      public class ProjectModel
{
    public List<string> BusinessPriority { get; set; }
    public string projectId { get; set; }
    public string sSessionValue
    {
        get;

        set;

    }     
}

Index.html: index html code I will get autopopulate value in the below text box.but model property is null.

    <div class="col-md-2">
                            <div class="col-md-11">
                                <label class="control-label">Project Id</label>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="form-group form-group-md">
                                @Html.TextBoxFor(model => model.projectmodel.projectId, new { @class = "form-control", id = "ProjectId",@readonly = "true" })

                            </div>

                        </div>

Controller: Home Controller code below Session variables [HttpPost]

    public ActionResult Index(CommonModel data)
    {

        // Assign value into session
         Session["ProjectId"] = data.projectmodel.projectId;
        // Redirect view

        return RedirectToAction("Financials");

    }
    public ActionResult Financials()
    {

        // Create object of model call for call properties and set value

        var vSessionValue = new CommonModel();
       // Session["ProjectId"] = Request["ProjectId"].ToString();

        try
        {

            // check existing value into session

            if ((Object)Session["ProjectId"] != null)

                // assign value into properties

                vSessionValue.projectmodel.sSessionValue =  Session["ProjectId"].ToString();

            else

                // if session expired than set custom message

                vSessionValue.projectmodel.sSessionValue = "Session Expired";

        }

        catch
        {



        }

        // return value to view

        return View(vSessionValue);

    }
  • An empty `catch` block is a famously bad idea. How do you know there isn't an exception being thrown that's preventing your model from being populated? When you debug this, does the code reach that point? What is the value being pulled from session state? – David Apr 18 '17 at 17:25
  • The exception has thrown that is Object reference not set to an instance of an object. – Priya Apr 18 '17 at 17:46
  • In that case it's a duplicate of the now-linked question. Nothing to do with session or even ASP.NET MVC really, just that you're trying to use a `null` object somewhere. This is exactly why empty `catch` blocks are a terrible idea. They literally mean "If there's any error, I don't want to know about it." Which makes it difficult for you to know about your errors. – David Apr 18 '17 at 17:48

0 Answers0