问题节能新记录到多个表从一个单一的视图 [英] Problem saving new records to multiple tables from a single view

查看:113
本文介绍了问题节能新记录到多个表从一个单一的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的数据库添加新记录到多个相关表,所有有一个ID为PK和其他表作为FKS的标识。我根据我对NerdDinner范例教程的方式,但我无法找到如何在数据库中添加新记录的任何实例时,多个表参与。

ModelState.IsValid试图保存新的记录由于某种原因失败时。我试着将它们添加如下图所示,单独更新各表,但不起作用。

我是新来的MVC,.NET和编程一般并不能找出我做错了。任何帮助是AP preciated。

  [HttpPost]
    公众的ActionResult创建(队队)
    {        团队T =团队;
        的UpdateModel(T);        如果(ModelState.IsValid)
        {            teamRepository.AddTeam(T);
            teamRepository.Save();            返回RedirectToAction(详细信息,新{ID = t.TeamID});
        }        返回RedirectToAction(创建);
    }

下面是我使用的视图模型

 公共类TeamViewModel
{
    //属性
    公共球队球队{搞定;组; }
    公众的IQueryable<&系GT; departmentsList {搞定;组; }
    公众的IQueryable<团队及GT;团队列表{搞定;组; }
    公众的SelectList countriesList {搞定;组; }
    国家公国{搞定;组; }
    //构造函数
    公共TeamViewModel()
    {    }    公共TeamViewModel(IQueryable的<团队及GT;团队)
    {
         团队列表=队伍;
    }    公共TeamViewModel(TeamViewModel VM)
    {
        团队= vm.team;
        departmentsList = vm.departmentsList;
    }    公共TeamViewModel(TeamViewModel VM,C国)
    {
        团队= vm.team;
        team.CountryID = c.CountryID;
    }    公共TeamViewModel(TeamViewModel VM,IEnumerable的<国家>国家)
    {
        团队=新的Team();
        countriesList =新的SelectList(国CountryID,短名称);
    }}

这是在情况下,查看它是需要

 <使用%(Html.BeginForm()){%GT;
    <%:Html.ValidationSummary(真)%GT;    <&字段集GT;    < D​​IV CLASS =编辑标记><%:Html.LabelFor(M = GT; m.team.ShortName)%GT;< / DIV>
    < D​​IV CLASS =主编场><%:Html.TextBoxFor(M = GT; m.team.ShortName)%GT;<%:Html.ValidationMessageFor(M = GT; m.team.ShortName ,*)%GT;< / DIV>    < D​​IV CLASS =编辑标记><%:Html.LabelFor(M = GT; m.team.FullName)%GT;< / DIV>
    < D​​IV CLASS =主编场><%:Html.TextBoxFor(M = GT; m.team.FullName)%GT;<%:Html.ValidationMessageFor(M = GT; m.team.FullName ,*)%GT;< / DIV>    < D​​IV CLASS =编辑标记><%:Html.LabelFor(M = GT; m.team.DateEstablished)%GT;< / DIV>
    < D​​IV CLASS =主编场><%:Html.TextBoxFor(M = GT; m.team.DateEstablished)%GT;<%:Html.ValidationMessageFor(M = GT; m.team.DateEstablished ,*)%GT;< / DIV>    < D​​IV CLASS =编辑标记><%:Html.LabelFor(M = GT; m.team.City)%GT;< / DIV>
    < D​​IV CLASS =主编场><%:Html.TextBoxFor(M = GT; m.team.City)%GT;<%:Html.ValidationMessageFor(M = GT; m.team.City ,*)%GT;< / DIV>    < D​​IV CLASS =编辑标记><%:Html.LabelFor(M = GT; m.team.Country)%GT;< / DIV>
//拼错场
//< D​​IV CLASS =主编场><%:Html.DropDownListFor(型号=> model.team.Country,Model.countriesList,< - 选择国家 - >中)% >< / DIV>
//修正后场
< D​​IV CLASS =主编场><%:Html.DropDownListFor(型号=> model.team.CountryID,Model.countriesList,< - 选择国家 - >中)%GT; < / DIV>< /字段集>&所述p为H.;
    <输入类型=提交值=保存/>
&所述; / P>


解决方案

首先什么库ORM是您使用?其次你为什么要重新映射领域一个新的团队对象。 MVC已经做到了这一点给你,我不认为你需要做到这一点。如果您使用的东西NHibernate的那么它可能与设置的国家ID,而不是对象的国家做。如果你有一个国家表,你可能已经有了一个赤贫的国家,因此NH将查找以下

team.Country.Id

和NOT

team.CountryId

看着你认为这是事实。如果我是你,我会重新映射团队对象,已经完成了。打破了行动,并检查什么是该国的对象。

希望这有助于

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

ModelState.IsValid fails when trying to save the new records for some reason. 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. 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

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? Secondly why are you remapping the fields to a new Team object. Mvc has already done this for you, I don't think you need to do this. If your using something NHibernate then it's probably to do with the setting the country Id and not the country object. If you have a country table the you've probably got a country abject and therefore NH will be look for the the following

team.Country.Id

AND NOT

team.CountryId

Looking at you view this is the case. If I were you I would remap the Team object, already done. Break on the action and check what's the country object.

Hope this helps

这篇关于问题节能新记录到多个表从一个单一的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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