0

我的域模型是具有一个或多个 CompanyAdmin 实体的公司实体。

我意识到,要在发布表单值时使用默认模型绑定器,CompanyAdmin 实体必须具有唯一的名称。

首次浏览该页面时,公司或公司管理员都没有数据。这意味着,此代码在视图中不起作用,因此不会显示输入文本框。

@Html.EditorFor(model => model.Company.CompanyContacts)

所以,为了让它们显示我使用:

@Html.EditorFor(model => model.Company.CompanyContacts.ElementAt(0).FirstName)

但是,现在的问题是模型绑定在 POST 时不起作用。我能找到的所有示例都使用已经创建的数据。

我怎样才能让它工作?

视图模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ContractorDirect.Models
{
  public class CompanyAdminViewModel
  {
    public Company Company { get; set; }
    public RegisterModel UserReg { get; set; }
  }
}

控制器

public ViewResult CompanyAdmin(int? id)
    {
        CompanyAdminViewModel cavm = new CompanyAdminViewModel();

        jobTitleRepository = new JobTitleRepository();
        companyContactRepository = new CompanyContactRepository();

        var jobtitles = jobTitleRepository.All;
        ViewBag.JobTitleId = new SelectList(jobtitles, "JobTitleId", "JobName");

        if (id.HasValue)
        {
            cavm.Company = companyRepository.Find((int)id);
            cavm.Company.CompanyContacts = companyContactRepository.All.Where(admin=>admin.IsCompanyAdmin).ToList();
            return View(cavm);
        }

        cavm.Company = new Company();
        cavm.Company.CompanyContacts = new List<CompanyContact>();
        cavm.UserReg = new RegisterModel();
        return View(cavm);
    }

    [HttpPost]
    public ActionResult CompanyAdmin(CompanyAdminViewModel cavm)
    {
        JobTitleRepository rep = new JobTitleRepository();
        var jobtitles = rep.All;
        ViewBag.JobTitleId = new SelectList(jobtitles, "JobTitleId", "JobName");

        cavm.UserReg.UserName = cavm.Company.CompanyContacts.ElementAt(0).Email;

        if (ModelState.IsValid)
        {
            AccountController account = new AccountController();
            account.Register(cavm.UserReg, "CompanyAdmin");
            companyRepository.InsertOrUpdate(cavm.Company);
            companyRepository.Save();
            return RedirectToAction("CompanyRegistrationConfirmation");
        }
        else
        {
            return View();
        }
    }

看法

  @model ContractorDirect.Models.CompanyAdminViewModel

  @{
       ViewBag.Title = "CompanyAdmin";
   }

 <h2>Company Admin</h2>

 <table>
 <tr style="vertical-align: top">
    <td>
        <div class="editor-label">
            @Html.LabelFor(model => model.Company.CompanyName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Company.CompanyName)
            @Html.ValidationMessageFor(model => model.Company.CompanyName)
        </div>

        <fieldset>
            <legend>Company Administrator</legend>



            <div class="editor-label">
                @Html.LabelFor(model => model.Company.CompanyContacts.ElementAt(0).JobTitleId)
            </div>
            <div class="editor-field">
                @Html.DropDownList("JobTitleId", string.Empty)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Company.CompanyContacts.ElementAt(0).FirstName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Company.CompanyContacts.ElementAt(0).FirstName)
                @Html.ValidationMessageFor(model => model.Company.CompanyContacts.ElementAt(0).FirstName)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Company.CompanyContacts.ElementAt(0).LastName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Company.CompanyContacts.ElementAt(0).LastName)
                @Html.ValidationMessageFor(model => model.Company.CompanyContacts.ElementAt(0).LastName)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Company.CompanyContacts.ElementAt(0).Email)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Company.CompanyContacts.ElementAt(0).Email)
                @Html.ValidationMessageFor(model => model.Company.CompanyContacts.ElementAt(0).Email)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Company.CompanyContacts.ElementAt(0).Phone)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Company.CompanyContacts.ElementAt(0).Phone)
                @Html.ValidationMessageFor(model => model.Company.CompanyContacts.ElementAt(0).Phone)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.UserReg.Password)
            </div>

            <div class="editor-field">
                @Html.EditorFor(model => model.UserReg.Password)
                @Html.ValidationMessageFor(model => model.UserReg.Password)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.UserReg.ConfirmPassword)
            </div>

            <div class="editor-field">
                @Html.EditorFor(model => model.UserReg.ConfirmPassword)
                @Html.ValidationMessageFor(model => model.UserReg.ConfirmPassword)
            </div>

        </fieldset>
    </td>
</tr>

4

1 回答 1

1

好的 - 问题出在视图中

这有效

@Html.EditorFor(model => model.Company.CompanyContacts[0].FirstName)

这不

@Html.EditorFor(model => model.Company.CompanyContacts.ElementAt(0).FirstName)
于 2013-02-02T16:16:43.177 回答