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")
}