Problem saving new records to multiple tables from a single view

眉间皱痕 提交于 2019-12-25 03:45:31

问题


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>

回答1:


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



来源:https://stackoverflow.com/questions/7179143/problem-saving-new-records-to-multiple-tables-from-a-single-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!