如何使用外键更新记录? [英] How do I update a record with a foreign key?

查看:115
本文介绍了如何使用外键更新记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有另一个表的外键的表。



我能够为Create操作创建MVC但我似乎无法想象更新操作。



我一直收到错误:处理请求时发生了未处理的异常。



ArgumentNullException:值不能为null。

参数名称:source



VM:

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:

[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:

[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:

<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>





我尝试过:



我曾试图谷歌如何使用外键属性.net核心更新记录,但没有发现任何有用的东西。



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.

推荐答案

您的UpdateCustomizedHTMLElement加载您正在更新的元素,但您永远不会对该元素进行任何更改并将其保存回数据库。就这么简单。



你也没有传递任何说这些都是改变的东西。您只需传入要加载的元素的ID。
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.


这篇关于如何使用外键更新记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆