繁体   English   中英

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

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

我正在尝试将数据库中的新记录添加到多个相关表中,这些表的ID为PK,其他表的ID为FK。 我的方法基于NerdDinner教程,但是当涉及多个表时,找不到如何在数据库中添加新记录的任何示例。

由于某种原因尝试保存新记录时,ModelState.IsValid失败。 我尝试如下所示添加它们,并分别更新每个表,但这不起作用。

我是MVC,.NET和编程的新手,无法弄清楚我在做什么错。 任何帮助表示赞赏。

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

这是我正在使用的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");
    }

}

这是在需要时使用的视图

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

首先,您正在使用什么存储库ORM? 其次,为什么要将字段重新映射到新的Team对象。 Mvc已经为您完成了此操作,我认为您不需要这样做。 如果您使用的是NHibernate,则可能与设置国家/地区ID(而非国家/地区对象)有关。 如果您有一个国家/地区表,那么您可能会有一个国家/地区表,因此NH将寻找以下内容

team.Country.Id

并不是

team.CountryId

看着你的看法就是这种情况。 如果我是你,我将重新映射Team对象,已经完成了。 暂停操作,检查国家对象是什么。

希望这可以帮助

暂无
暂无

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

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