-2

I am starting on ASP.net and I can not resolve the next problem.

System.NullReferenceException: La référence d'objet n'est pas définie à une instance d'un objet.

I forwards a User Id from User controller to this Action, who belongs to Email Controler. Then I want to give this Id to a new parameter.

    public ActionResult Create(Guid? id)
    {
        ViewBag.Key_Destinataire = id;
        return View();
    }

Le code de ma page HTML : @model ProjetWeb.Models.Email

////
        @Html.LabelFor(model => model.Objet, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Objet, new { htmlAttributes = new { @class = "form-control" } })
///

    <div class="form-group">
        @Html.LabelFor(model => model.Key_Destinataire, "Key_Destinataire", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">



            @Model.Key_Destinataire.Value=ViewBag.Key_Destinataire;

            @Html.ValidationMessageFor(model => model.Key_Destinataire, "", new { @class = "text-danger" })
        </div>
    </div>

English Translation;


Hello,

I start on ASP.net and I can not resolve the error below:

System.NullReferenceException : Object reference not set to an instance of an object.

my code below: I transmits the ID of a user of controller User controller in this action the Email. Then I want to assign the id to a new setting.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
bast
  • 9
  • 1
  • 6

2 Answers2

1

Your '@Model' is null. You didn't pass any model in controller to view.

Some reading: check this

Alexandru Chichinete
  • 1,166
  • 12
  • 23
0

You will need to pass the id from the controller action to the view in your return statement. For example:

public ActionResult Create(Guid? id)
{
    return View(id);
}

This will now be available to you in your view if you declare @model Guid?. For example:

@model Guid?

<div class="form-group">
    @Html.LabelFor(model => model, "Key_Destinataire", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextBoxFor(model => model.Value)
        @Html.ValidationMessageFor(model => model, "", new {@class = "text-danger"})
    </div>
</div>
chxzy
  • 489
  • 4
  • 13