1

I am a beginner in coding. I have been asked to make an application in VisualStudio using ASP.Net, MVC.

I have created a view where you can display a table from the database. Now I have to be able to Edit the data in another view and in the same view.

When I create a new view, I have this error when I try to run it:

The parameters dictionary contains a null entry for parameter 'EmailId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32, System.String, System.String, System.String, System.String)' in 'MyMVCApplication.Controllers.EmailTemplateController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

I can not figure out what the problem is in the controller as I am just two months learning so I am still working on the basics.

This is my code:

Controller:

public ActionResult Edit (int EmailId, string userName, string title, string Email, string description)
{          
    UpdateDataBase(EmailId, userName, title, Email, description);
    return View("EmailData");
}

[HttpPost]
public ActionResult Edit(ModelTemplateEmail  EditEmailData)
{       
    if (ModelState.IsValid)
    {                
        return RedirectToAction("EmailData");
    };

    return View(EditEmailData);
}

view:

@using (Html.BeginForm("Edit", "EmailTemplate", new { Id = Model.EmailData }, FormMethod.Post, null))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>ModelTemplateEmail</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.EmailId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EmailId, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EmailId, "", new { @class = "text-danger" })
            </div>
        </div>

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

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

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

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

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

        @Html.HiddenFor(model => model.EmailData)

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "EmailData")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Lisa
  • 31
  • 7
  • Start with one action (you have 4 (including overloads), which may be confusing the issue). It looks like you're calling the first (GET) Edit. This has a number of parameters that must be provided - while the `string` parameters will accept null, the `int` parameter won't - so it's about how you are opening your edit page - you've provided the view, but this doesn't show how you get to the view in the first place - ie what calls `../Edit?EditId=1` – freedomn-m Mar 20 '20 at 21:50
  • Thanks for the answer! So this means I have to make the int to accept null? – Lisa Mar 22 '20 at 12:42
  • No - it means you need to pass it in as a parameter when you open the page. – freedomn-m Mar 22 '20 at 17:00
  • ok. I do not know how to do that but i will investigate. thanks for the advice! – Lisa Mar 23 '20 at 08:37
  • How do you open the page? `http://mysite/mycontroller/edit` ? – freedomn-m Mar 23 '20 at 08:38
  • 1
    @Html.ActionLink("Edit", "EditData", new { id = item.EmailId },null) | – Lisa Mar 23 '20 at 08:47

1 Answers1

0

The @HtmlAction new { paramterName = must match the parameter name on the action Action(int paramaterName - in this case EmailId

The parameters dictionary contains a null entry for parameter 'EmailId' of non-nullable type

@Html.ActionLink("Edit", "EditData", new { id = item.EmailId }, null) 
                                           ^^

and

public ActionResult Edit(int EmailId, 
                             ^^^^^^^

The parameter name is EmailId but your ActionLink is using id.

If you look in the rendered html (on the client), it will be creating a link like ..Edit?id=123 but what you need to call the action is a link like ..Edit?EditId=123.

You can either change your action signature to

public ActionResult Edit(int id, 

or change your ActionLink to:

@Html.ActionLink("Edit", "EditData", new { EmailId = item.EmailId }, null) 
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • ohh thanks for the good explanation. now I understand! but now I have another error when connecting with my database i tried to igure out but i dont d¡find the error i guess is simple but i am still learning the basics – Lisa Mar 23 '20 at 09:11
  • ``var sqlstring = string.Format("INSERT INTO Email (Email, Description, UserName, Title) " + "Values ('{0}', '{1}', '{2}', '{3}')", Email, description, userName, title);`` – Lisa Mar 23 '20 at 09:19
  • `var myConnection = getconection(); SqlCommand myCommand = new SqlCommand(sqlstring, myConnection); myCommand.ExecuteNonQuery();` – Lisa Mar 23 '20 at 09:21
  • That's ok, if the issue with null EmailId is resolved, please upvote the answer and mark as the correct answer. If you have further questions, please ask a new question (you'll get a wider audience), you can always add a link here to the new question. – freedomn-m Mar 23 '20 at 09:21
  • when i connect i have an error that says that i have a incorrect syntax near ('. incorrect syntax near the kewword SET but i can not figure aout my syntax errors – Lisa Mar 23 '20 at 09:22
  • You'll need to use "paramertised queries" to reduce SQL injection etc - here's an example answer: https://stackoverflow.com/a/12142974/2181514 – freedomn-m Mar 23 '20 at 09:22
  • You probably have a `'` character in one of your string fields - paramertised query would resolve that. – freedomn-m Mar 23 '20 at 09:23
  • https://stackoverflow.com/questions/60811442/update-edit-delete-and-import-file-javascript-in-visualstudio-using-aspnet-m – Lisa Mar 23 '20 at 10:18