asp.net核心创建/更新外键 [英] asp.net core creating/updating a foreignkey

查看:148
本文介绍了asp.net核心创建/更新外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在"Todo"类中添加了"Pharmacy"类的foreignKey.当我添加或编辑待办事项时,无法链接任何药房(通过选择框).这是我的模型:

I added to my "Todo" class a foreignKey to my "Pharmacy" class. When I add or edit a Todo, no Pharmacy can be linked (from a selectbox). Here's my model:

public class Todo
    {
        [Key]
        public int ID { get; set; }

        public Pharmacy Pharmacy { get; set; }
    }

public class Pharmacy
    {
        [Key]
        public int PharmacyID { get; set; }

        public string Name { get; set; }

        public string Pharmacist { get; set; }

        [DataType(DataType.PhoneNumber)]
        public int Phone { get; set; }

        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        public int NPA { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string Canton { get; set; }
    }

我的代码列表列出了我的待办事项上的所有药房:

My code's listing to listing all Pharmacies on my Todo:

public class PharmacynamePageModel : PageModel
    {
        public SelectList PharmacyNameSL { get; set; }

        public void PopulatePharmacysDropDownList(ApplicationDbContext _context, object selectedPharmacy = null)
        {
            var query = (from p in _context.Pharmacy orderby p.Name select p).ToList();

            PharmacyNameSL = new SelectList(query, "PharmacyID", "Name", selectedPharmacy);
        }
    }

好吧,在这一步,我在HTML表单上输入了以下代码:

Well, at this step, i have this code on my HTML form:

<label asp-for="Todo.Pharmacy" class="control-label"></label>
                <select asp-for="Todo.Pharmacy" class="form-control"
                        asp-items="Model.PharmacyNameSL">
                    <option value="">-- Select a Pharmacy --</option>
                </select>
                <span asp-validation-for="Todo.Pharmacy" class="text-danger" />

和我的PostAsync()到我的创建和编辑控制器中:

and my PostAsync() into my create and edit controllers:

public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            var emptyTask = new Todo();

            if (await TryUpdateModelAsync(emptyTask, "Todo",  
                s => s.ID, s => s.OwnerID, 
                s => s.Title, s => s.Description,
                s => s.DueDate,
                s => s.Pharmacy,
                s => s.Priority))
            {
                emptyTask.Status = EnumStatus.Open;
                _context.Todo.Add(emptyTask);
                await _context.SaveChangesAsync();
                if (emptyTask.OwnerID != HttpContext.User.Identity.Name)
                {
                    LogVM.AddLog("Todos", $"{HttpContext.User.Identity.Name} attributed a new task to {emptyTask.OwnerID}.", _context);
                }
                return RedirectToPage("./Index");
            }

            PopulatePharmacysDropDownList(_context, emptyTask.Pharmacy);

            return RedirectToPage("./Index");
        }

所有数据均已映射/绑定,但不是药房字段,该字段的值始终为空.

All data are mapped/binded, but not the pharmacy field who's the value are always null.

希望您能帮助我:).

谢谢你.

推荐答案

发布后,数据绑定会将Select的"dataValueField"绑定到

On Post, Data Binding will bind the Select's "dataValueField" to the model's data member.

您正在尝试将整个对象映射到Select的选定项目.您已经提到,只有Pharmacy字段为空-这很可能是因为从您提供的代码看来,这是您可能要尝试修改的唯一字段.其他项目的值可能是在Page代码本身中设置的(&不受Post上数据的约束).

You are trying to map an entire object to the Select's selected item. You have mentioned that only the Pharmacy field is null - it is likely because from your provided code it looks like that's the only field you are probably trying to modify. The values for the other items are probably setup in the Page code itself (& not being data bound on Post).

我建议您为所选的药房"项目创建一个单独的字段,并将其绑定到选择"框.

I would suggest that you create a separate field for the selected "Pharmacy" item and bind that to the Select box.

在PharmacynamePageModel类中,创建一个公共的可绑定"属性,如下所示:

Inside your PharmacynamePageModel class, create a pubic "bindable" property, like so:

 [BindProperty]
 public string SelectedPharmacy { get; set; }

然后在Razor代码上,而不是绑定到"Pharmacy",而是绑定到"SelectedPharmacy"(在asp-for属性中),如下所示:

And then on the Razor code, instead of binding to "Pharmacy", bind to "SelectedPharmacy" (in the asp-for attribute), like so:

                <select asp-for="SelectedPharmacy" class="form-control"
                        asp-items="Model.PharmacyNameSL">
                    <option value="">-- Select a Pharmacy --</option>
                </select>
                <span asp-validation-for="Todo.Pharmacy" class="text-danger" />

现在,在表格发布中,所选药房ID应该可用.您可以根据自己的意愿使用ID.

Now, on Form post, the selected Pharmacy Id should be available. It is up to you to make use of the Id as you wish.

希望这行得通.如果是这样,请将其标记为答案.

Hope this works. If it does, please mark this as the answer.

这篇关于asp.net核心创建/更新外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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