Click here to Skip to main content
15,888,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table with a foreign key to another table.

I was able to create the MVC for the Create action but I can't seem to figure out the Update action.

I keep getting an error: An unhandled exception occurred while processing the request.

ArgumentNullException: Value cannot be null.
Parameter name: source

VM:
C#
[Required]
[Display(Name = "Name")]
public string Name { get; set; }

[Required]
[Display(Name = "Description")]
public string Description { get; set; }

[Required]
[Display(Name = "Code before element including opening tag")]
public string Before { get; set; }

[Required]
[Display(Name = "Code after element including closing tag")]
public string After { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    if (Before == null || After == null)
        yield return new ValidationResult("Custom HTML Before and Custom HTML After must be specified");
}

[Required]
[Display(Name = "HTML Element")]
public int HTMLElement { get; set; }

public ICollection<HTMLElement> HTMLElements { get; set; }


Controller:
C#
[HttpGet]
public async Task<IActionResult> UpdateCustomizedHTMLElement(int Id)
{
    var eElement = await db.CustomizedElements
.Include(e => e.HTMLElement)
.ThenInclude(e => e.Element)
.SingleOrDefaultAsync(w => w.Id == Id);
    return View(eElement);
}

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult UpdateCustomizedHTMLElement(HTMLElement updateHTMLElement)
{
    return View(updateHTMLElement);
}


View:

C#
<div class="row justify-content-md-center">
    <div class="col-sm-10 col-md-7 col-lg-6">
        <h2>Update Customized HTML Element Type</h2>
        <form method="post" asp-controller="Templates" asp-action="UpdateCustomizedHTMLElement">
            <div class="form-group">
                <label asp-for="Name"></label>:
                <input class="form-control form-control-sm" type="text" asp-for="Name" />
                <span class="text-danger" asp-validation-for="Name"></span>
            </div>
            <div class="form-group">
                <label asp-for="Before"></label>:
                <input class="form-control form-control-sm" type="text" asp-for="Before" />
                <span class="text-danger" asp-validation-for="Before"></span>
            </div>
            <div class="form-group">
                <label asp-for="HTMLElement"></label>:
                @Html.DropDownListFor(m => m.HTMLElement, new SelectList(Model.HTMLElement.Element, "Id", "Element"), "", new { @class = "form-control" })
                <span class="text-danger" asp-validation-for="HTMLElement"></span>
            </div>
            <div class="form-group">
                <label asp-for="After"></label>:
                <input class="form-control form-control-sm" type="text" asp-for="After" />
                <span class="text-danger" asp-validation-for="After"></span>
            </div>
            <div>
                <button class="btn btn-primary" type="submit" value="Submit">Save</button>
                <button class="btn btn-secondary" type="button" value="Cancel" onclick="location.href='@Url.Action("HTMLElements", "Templates")'">Cancel</button>
            </div>
        </form>
    </div>
</div>


What I have tried:

I have tried to Google "how to update a record with a foreign key property .net core" but haven't found anything that works.
Posted
Updated 24-Sep-17 18:06pm

1 solution

Your UpdateCustomizedHTMLElement loads the element you're apparently updating, but you never make any changes to that element and save it back to the database. It's that simple.

You're also not passing in anything that says "these are the changes to make". You're just passing in the ID of the element to load.
 
Share this answer
 
Comments
Member 13427316 25-Sep-17 9:00am    
There's no save at this point because the record isn't even being loaded for [httpGet]. After I get that to work I'll work on the Post method.

For now I'm getting that error just trying to load the object.

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