我如何 return 从视图上的表单到控制器的 viewModel

How would I return a viewModel from a form on a view to the Controller

我的模型是这样的:

银行其他律师事务所
 | | |
银行联系其他联系律师事务所联系
 |______________|________________|
                |
                |
              联系人 

因此,在创建联系人时,我试图传递信息以创建特定关联实体的条目。目前我通过 RelationId 和 ContactId 然后尝试在控制器中创建一个新实体但是我的“表单上的保存按钮没有触发。我认为这可能是因为我没有正确绑定信息。我”我也张贴这个看看这是否是处理这种情况的正确方法

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Save(ContactFormViewModel viewModel)
    {
        if (!ModelState.IsValid)
        {
            return View("ContactForm", viewModel);
        }
        if (viewModel.Contact.Id == 0)
        {
            _context.Contacts.Add(viewModel.Contact);
            if (viewModel.IsBank)
            {
                var bankContact = new BankContact()
                {
                    BankId = viewModel.RelationId,
                    Bank = _context.Banks.Single(b => b.Id == viewModel.RelationId),
                    ContactId = viewModel.ContactId,
                    Contact = viewModel.Contact
                };
                _context.BankContacts.Add(bankContact);
            }
            else if (viewModel.IsLawFirm)
            {
                var lawFirmContact = new LawFirmContact()
                {
                    LawFirmId = viewModel.RelationId,
                    LawFirm = _context.LawFirms.Single(l => l.Id == viewModel.RelationId),
                    ContactId = viewModel.ContactId,
                    Contact = viewModel.Contact,

                };
                _context.LawFirmContacts.Add(lawFirmContact);
            }
            else if (viewModel.IsOther)
            {
                var standaloneContact = new StandaloneContact()
                {
                    StandAloneId = viewModel.RelationId,
                    Standalone = _context.Standalones.Single(s => s.Id == viewModel.RelationId),
                    Contact = viewModel.Contact,
                    ContactId = viewModel.ContactId
                };
                _context.StandaloneContacts.Add(standaloneContact);
            }
        }

        else
        {
            var contactInDb = _context.Contacts.Single(c => c.Id == viewModel.Contact.Id);

            contactInDb.Firstname = viewModel.Contact.Firstname;
            contactInDb.Surname = viewModel.Contact.Surname;
            contactInDb.Email = viewModel.Contact.Email;
            contactInDb.ContactNo = viewModel.Contact.ContactNo;

            if (!String.IsNullOrWhiteSpace(viewModel.Contact.AltContactNo))
                contactInDb.AltContactNo = viewModel.Contact.AltContactNo;            
        }

        _context.SaveChanges();
        return RedirectToAction("Index", "Contacts");

    }

<div class="card">
    <div class="card-body">
        @using (Html.BeginForm("Save", "Contacts"))
        {

        <div class="form-row">
            <div class="col-3">
                <div class="form-group">
                    @Html.LabelFor(c => c.Contact.Firstname, new { @class = "form-control-label" })
                    @Html.TextBoxFor(c => c.Contact.Firstname, new { @class = "form-control" })
                    @Html.ValidationMessageFor(c => c.Contact.Firstname)
                </div>
            </div>

            <div class="col-3">
                <div class="form-group">
                    @Html.LabelFor(c => c.Contact.Surname, new { @class = "form-control-label" })
                    @Html.TextBoxFor(c => c.Contact.Surname, new { @class = "form-control" })
                    @Html.ValidationMessageFor(c => c.Contact.Surname)
                </div>
            </div>
        </div>

            <div class="form-row">

            <div class="col-3">
                <div class="form-group">
                    @Html.LabelFor(c => c.Contact.Email, new { @class = "form-control-label" })
                    @Html.TextBoxFor(c => c.Contact.Email, new { @class = "form-control" })
                    @Html.ValidationMessageFor(c => c.Contact.Email)
                </div>
            </div>
            <div class="col-3">
                <div class="form-group">
                    @Html.LabelFor(c => c.Contact.Birthday, new { @class = "form-control-label" })
                    @Html.TextBoxFor(c => c.Contact.Birthday, new { @class = "form-control datepicker-here", @data_language = "en", @autocomplete = "off" })
                </div>
            </div>
        </div>
            <div class="form-row">
                <div class="col-3">
                    <div class="form-group">
                        @Html.LabelFor(c => c.Contact.ContactNo, new { @class = "form-control-label" })
                        @Html.TextBoxFor(c => c.Contact.ContactNo, new { @class = "form-control" })
                        @Html.ValidationMessageFor(c => c.Contact.ContactNo)
                    </div>
                </div>

                <div class="col-3">
                <div class="form-group">
                    @Html.LabelFor(c => c.Contact.AltContactNo, new { @class = "form-control-label" })
                    @Html.TextBoxFor(c => c.Contact.AltContactNo, new { @class = "form-control" })
                </div>
                    </div>

            </div>

            <div class="form-row">
                <div class="col-4">
                    <div class="form-group">
                        <input type="radio" name="choice-contact-type" id="choice-contact-type-bank" required>
                        <label for="choice-contact-type-bank">Bank</label>

                        <div class="reveal-if-active">

                            @Html.DropDownListFor(b => b.BankContact.BankId, new SelectList(Model.Banks, "Id", "Name "), "", new { @class = "form-control" })
                            @{Model.IsBank = true;}
                        </div>
                    </div>
                </div>

                <div class="col-4">
                    <div class="form-group">
                        <input type="radio" name="choice-contact-type" id="choice-contact-type-lawFirm">
                        <label for="choice-contact-type-lawFirm">Law Firm</label>

                        <div class="reveal-if-active">

                            @Html.DropDownListFor(l => l.LawFirmContact.Id, new SelectList(Model.LawFirms, "Id", "Name"), "", new { @class = "form-control" })
                            @{Model.IsLawFirm = true;}
                        </div>
                    </div>
                </div>

                <div class="col-4">
                    <div class="form-group">
                        <input type="radio" name="choice-contact-type" id="choice-contact-type-standalone">
                        <label for="choice-contact-type-standalone">Other</label>

                        <div class="reveal-if-active">

                            @Html.DropDownListFor(s => s.StandaloneContact.Id, new SelectList(Model.Standalones, "Id", "Name"), "", new { @class = "form-control" })
                            @{Model.IsOther = true;}
                        </div>
                    </div>
                </div>
            </div>

            @Html.AntiForgeryToken()
            <button type="submit" class="btn btn-primary">Save</button>

        }
    </div>
</div>

编辑

隐藏的下拉字段是必需的,因此验证不允许它通过,因为它们是隐藏的我看不到!该字段是必需的,因为在我的 StandaloneContact Id 下是必需的。它总是必需的,那么我将如何解决这个问题?将其设置为默认值?

我仍然想知道这是否是将一个模型link 转换为具有多对多关系的 3 个不同模型的最佳方法。在 MVC 中还有其他方法可以做到这一点吗?

尝试将其添加到表单中

@using (Html.BeginForm("Save", "Contacts", FormMethod.Post))

另外,关于将数据从视图发送到控制器的正确方法,这主要是您在给定时间所需要的。如果您只是想以一种形式发送数据,这可能是最好的方法。但是,如果你,例如,发送这个数据,然后刷新页面的某个方面,比如局部视图,我会建议使用 ajax.

编辑:根据对问题的编辑,通过使独立联系人 ID 可以为空,这应该可以解决始终需要 ID 的问题