简体   繁体   English

从单个视图将新记录保存到多个表时出现问题

[英]Problem saving new records to multiple tables from a single view

I am trying to add new records in my DB to multiple related tables that all have an ID as PK and IDs of other tables as FKs. 我正在尝试将数据库中的新记录添加到多个相关表中,这些表的ID为PK,其他表的ID为FK。 I've based my approach on the NerdDinner tutorial but I can't find any examples on how to add new records in the DB when more than one table is involved. 我的方法基于NerdDinner教程,但是当涉及多个表时,找不到如何在数据库中添加新记录的任何示例。

ModelState.IsValid fails when trying to save the new records for some reason. 由于某种原因尝试保存新记录时,ModelState.IsValid失败。 I tried adding them as shown below and update each table seperately but that doesn't work. 我尝试如下所示添加它们,并分别更新每个表,但这不起作用。

I am new to MVC, .NET and programming in general and can't figure out what I am doing wrong. 我是MVC,.NET和编程的新手,无法弄清楚我在做什么错。 Any help is appreciated. 任何帮助表示赞赏。

[HttpPost]
    public ActionResult Create(Team team)
    {

        Team t = team;
        UpdateModel(t);

        if (ModelState.IsValid)
        {

            teamRepository.AddTeam(t);
            teamRepository.Save();

            return RedirectToAction("Details", new { id = t.TeamID });
        }

        return RedirectToAction("Create");
    }

Here is the ViewModel I am using 这是我正在使用的ViewModel

public class TeamViewModel
{
    //Properties
    public Team team { get; set; }
    public IQueryable<Department> departmentsList { get; set; }
    public IQueryable<Team> teamList { get; set; }
    public SelectList countriesList { get; set; }
    public Country country { get; set; }
    //Constructors
    public TeamViewModel()
    {

    }

    public TeamViewModel(IQueryable<Team> teams)
    {
         teamList = teams;
    }

    public TeamViewModel(TeamViewModel vm)
    {
        team = vm.team;
        departmentsList = vm.departmentsList;
    }

    public TeamViewModel(TeamViewModel vm, Country c)
    {
        team = vm.team;
        team.CountryID = c.CountryID;
    }

    public TeamViewModel(TeamViewModel vm, IEnumerable<Country> countries)
    {
        team = new Team();
        countriesList = new SelectList(countries, "CountryID", "ShortName");
    }

}

And this is the View in case it is needed 这是在需要时使用的视图

<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>

    <div class="editor-label"><%: Html.LabelFor(m => m.team.ShortName) %></div>
    <div class="editor-field"><%: Html.TextBoxFor(m => m.team.ShortName) %><%: Html.ValidationMessageFor(m => m.team.ShortName, "*") %></div>

    <div class="editor-label"><%: Html.LabelFor(m => m.team.FullName)%></div>
    <div class="editor-field"><%: Html.TextBoxFor(m => m.team.FullName)%><%: Html.ValidationMessageFor(m => m.team.FullName, "*")%></div>

    <div class="editor-label"><%: Html.LabelFor(m => m.team.DateEstablished)%></div>
    <div class="editor-field"><%: Html.TextBoxFor(m => m.team.DateEstablished)%><%: Html.ValidationMessageFor(m => m.team.DateEstablished, "*")%></div>

    <div class="editor-label"><%: Html.LabelFor(m => m.team.City)%></div>
    <div class="editor-field"><%: Html.TextBoxFor(m => m.team.City)%><%: Html.ValidationMessageFor(m => m.team.City, "*")%></div>

    <div class="editor-label"><%: Html.LabelFor(m => m.team.Country) %></div>
//The misspelled field        
//<div class="editor-field"><%: Html.DropDownListFor(model => model.team.Country, Model.countriesList, "<--Select Country-->") %></div>
//The corrected field  
<div class="editor-field"><%: Html.DropDownListFor(model => model.team.CountryID, Model.countriesList, "<--Select Country-->") %></div>

</fieldset>

<p>
    <input type="submit" value="Save" />
</p>

Firstly what Repository ORM are you using? 首先,您正在使用什么存储库ORM? Secondly why are you remapping the fields to a new Team object. 其次,为什么要将字段重新映射到新的Team对象。 Mvc has already done this for you, I don't think you need to do this. Mvc已经为您完成了此操作,我认为您不需要这样做。 If your using something NHibernate then it's probably to do with the setting the country Id and not the country object. 如果您使用的是NHibernate,则可能与设置国家/地区ID(而非国家/地区对象)有关。 If you have a country table the you've probably got a country abject and therefore NH will be look for the the following 如果您有一个国家/地区表,那么您可能会有一个国家/地区表,因此NH将寻找以下内容

team.Country.Id team.Country.Id

AND NOT 并不是

team.CountryId team.CountryId

Looking at you view this is the case. 看着你的看法就是这种情况。 If I were you I would remap the Team object, already done. 如果我是你,我将重新映射Team对象,已经完成了。 Break on the action and check what's the country object. 暂停操作,检查国家对象是什么。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM