0

In my ASP.NET MVC5 with Entity Framework project, I have two tables Account, AccountDetail (AccountDetail references to Account) with two corresponding models AccountModel and AccountDetailModel.

In a view(ex: RegistrationView), how could I save inputed data in the view into two tables above. Because when creating the RegistrationView by right click on the controller, I am able to choose only one model for it.

2 Answers 2

2

You can link AccountModel and AccountDetailModel, and then use AccountModel:

public AccountModel
{
    public int Id { get; set; }

    public string Name { get; set; }

    public IList<AccountDetailModel> Details { get; set; }
}

public AccountDetailModel
{
    public int Id { get; set; }

    public string Description { get; set; }
}

In the view:

@model AccountModel
. . .
@Html.HiddenFor(m => m.Id)
@Html.TextboxFor(m => m.Name)
@for (int i = 0; i < Model.Details.Count; i++)
{
    @Html.HiddenFor(m => m.Details[i].Id)
    @Html.TextboxFor(m => m.Details[i].Description)
}

In the action some code like this:

[HttpPost]
public ActionView Insert(AccountModel model)
{
    using (var dbContext = new DbContext())
    {
        var account = new Account();
        account.Id = model.Id;
        account.Name = model.Name;
        account.Details = new List<AccountDetail>();

        dbContext.AddObject(account);

        foreach (var modelDetail in model.Details)
        {
            var accountDetail = new AccountDetail();
            accountDetail.Id = modelDetail.Id;
            accountDetail.Description = modelDetail.Description;
            account.Details.Add(accountDetail);

            dbContext.AddObject(accountDetail);
        }

        dbContext.SaveChanges();
    }
}

If you want to update the record, you'll need some code to check, if the related detail record should be inserted or updated.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you too much for your nice answer. I will try this solution.
This is the right answer: AccountModel is the aggregate here, and really the model in this MVC; AccountDetailModel is just a convenient decomposition of AccountModel. You should accept this as the answer.
0

I am not sure if I understand you correctly but If you want to have both your models in the same view then just create a class like

public class RegistrationViewModel
{
    public AcountModel AcountModel { get; set; }
    public AcountDetailModel AcountDetailModel { get; set; }
}

and use this class for the viewmodel

2 Comments

I used to do as you, in my controller I create a class: public class Account_AccountDetails_Model { public Account account { get; set; } public AccountDetails accountDetails{get; set;} } . And declare model in the view: @model SBWebsite.Controllers.AccountController.Account_AccountDetails_Model but when invoking @model. I see a message The name model does not exist in the current context.
Really sorry for the format. I don't know how to make my question esier to see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.