1

次のモデルを受け入れるビューがあります。

Inherits="System.Web.Mvc.ViewPage<MVC_WordsByME.Models.JobCreationModel>"

これは、次のアクションにポストバックします。

    [HttpPost]
    public ActionResult Create(FormCollection formValues)
    {
        var job = new JobCreationModel();
        if (TryUpdateModel(job))
        {
            _jobRepository.AddJob(job);
            _jobRepository.Save();
            return RedirectToAction("Index");
        }
        return View(job);
    }

ただし、ポストバックすると、次の例外がスローされます。

タイプ 'System.Int32' のオブジェクトをタイプ 'System.String' にキャストできません。

説明: 現在の Web 要求の実行中に未処理の例外が発生しました。エラーの詳細とコード内のどこでエラーが発生したかについては、スタック トレースを確認してください。

例外の詳細: System.InvalidCastException: タイプ 'System.Int32' のオブジェクトをタイプ 'System.String' にキャストできません。

ソース エラー:

Line 135:        {
Line 136:            var job = new JobCreationModel();
Line 137:            if (TryUpdateModel(job))
Line 138:            {
Line 139:                _jobRepository.AddJob((Job)job);

ここでは強調できませんが、例外が発生するのは 137 行目です。このメソッドにステップインできないので、この例外の原因を特定するにはどうすればよいですか?

また、TryUpdateModelは、結果を反映するためにtrueまたはfalseを返すだけで、例外から私を保護することになっていませんか? 例外がスローされる可能性があることに気づきませんでした。

更新:モデルは次のとおりです(派生モデルとベースモデルの両方):

public class JobCreationModel : Job
{
    //
    // Properties

    public SelectList ClientsList { get; private set; }
    public SelectList Languages { get; set; }
    public SelectList Users { get; set; }


    //
    // Constructors

    public JobCreationModel()
    {
        var userCurrent = Membership.GetUser();

        SentDate = DateTime.Now;
        WorkedBy = userCurrent != null ? userCurrent.UserName : string.Empty;
        DeadlineDate = DateTime.Now;
        ReceivedDate = DateTime.Now;

        var clientRepository = new ClientRepository();
        ClientsList = new SelectList(clientRepository.GetAllClients(), "ID", "OrganisationName");

        var languageRepository = new LanguageRepository();
        Languages = new SelectList(languageRepository.GetAllLanguages(), "ID", "Code");

        var userList = Membership.GetAllUsers();
        Users = new SelectList(userList.Cast<MembershipUser>().ToList(), "UserName", "UserName", userCurrent);
    }

    public JobCreationModel(Job job)
    {
        var userCurrent = Membership.GetUser();

        Client = job.Client;
        ClientName = job.ClientName;
        DeadlineDate = job.DeadlineDate;
        FixedCost = job.FixedCost;
        ID = job.ID;
        Invoice = job.Invoice;
        JobDescription = job.JobDescription;
        JobFileName = job.JobFileName;
        LanguageFrom = job.LanguageFrom;
        LanguageTo = job.LanguageTo;
        PONumber = job.PONumber;
        ReceivedDate = job.ReceivedDate;
        SentDate = job.SentDate;
        WordCost = job.WordCost;
        WordCount = job.WordCount;
        WorkedBy = job.WorkedBy;

        var clientRepository = new ClientRepository();
        ClientsList = new SelectList(clientRepository.GetAllClients(), "ID", "OrganisationName");

        var languageRepository = new LanguageRepository();
        Languages = new SelectList(languageRepository.GetAllLanguages(), "ID", "Code");

        var userList = Membership.GetAllUsers();
        Users = new SelectList(userList.Cast<MembershipUser>().ToList(), "UserName", "UserName", userCurrent);
    }
}

基本型はエンティティであり、それを表示する最善の方法は、その背後にある XML を表示することです。

<EntityType Name="Jobs">
      <Key>
        <PropertyRef Name="ID" />
      </Key>
      <Property Name="ID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
      <Property Name="JobDescription" Type="nvarchar" Nullable="false" MaxLength="200" />
      <Property Name="ReceivedDate" Type="datetime" Nullable="false" />
      <Property Name="DeadlineDate" Type="datetime" Nullable="false" />
      <Property Name="SentDate" Type="datetime" Nullable="false" />
      <Property Name="Invoice" Type="int" />
      <Property Name="WordCount" Type="int" Nullable="false" />
      <Property Name="WordCost" Type="float" Nullable="false" />
      <Property Name="FixedCost" Type="float" Nullable="false" />
      <Property Name="Client" Type="int" Nullable="false" />
      <Property Name="JobFileName" Type="nvarchar" Nullable="false" MaxLength="500" />
      <Property Name="WorkedBy" Type="varchar" Nullable="false" MaxLength="50" />
      <Property Name="PONumber" Type="varchar" Nullable="false" MaxLength="50" />
      <Property Name="LanguageFrom" Type="int" Nullable="false" />
      <Property Name="LanguageTo" Type="int" Nullable="false" />
    </EntityType>

これには、それを拡張するバディ クラスがあります。

[MetadataType(typeof(JobValidation))]
[Bind(Include = "Client,SentDate,JobFileName,JobDescription,WordCost,WordCount,WorkedBy")]
public partial class Job
{
    public IEnumerable Clients
    {
        get
        {
            var clientRepository = new ClientRepository();
            return clientRepository.GetAllClients();
        }
    }
    public string ClientName { get; set; }
    public string SelectedMonth { get; set; }
    public string SelectedYear { get; set; }
}

public class JobValidation
{
    [Required(ErrorMessage = "Please select a client for the sent job")]
    [Range(1, 999999, ErrorMessage = "Please select a client")]
    public int Client { get; set; }

    [Required(ErrorMessage = "Please enter the completion date for this job")]
    [DataType(DataType.Date, ErrorMessage = "The date entered is not in a recognised format")]
    public DateTime SentDate { get; set; }

    [Required(ErrorMessage = "Job file must have a name")]
    [StringLength(500, ErrorMessage = "Job file name must not be longer than 500 characters")]
    public string JobFileName { get; set; }

    [Required(ErrorMessage = "Job must have a name")]
    [StringLength(200, ErrorMessage = "Job name must not be longer than 200 characters")]
    public string JobDescription { get; set; }

    [Required(ErrorMessage = "Please enter the word cost for the sent job")]
    [StringLength(6, ErrorMessage = "The word cost should not exceed 5 digits")]
    [DataType(DataType.Currency, ErrorMessage = "The word cost was not recognised as an amount of currency")]
    public string WordCost { get; set; }

    [Required(ErrorMessage = "Please enter the word count for the sent job")]
    [StringLength(8, ErrorMessage = "The word count must not exceed 99999999")]
    public string WordCount { get; set; }

    public string WorkedBy { get; set; }
}

最後に、ビューの関連部分を次に示します。

    <% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Job Details</legend>

        <div class="editor-label">
            Job description
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.JobDescription)%>
            <%: Html.ValidationMessageFor(model => model.JobDescription)%>
        </div>

        <div class="editor-label">
            PO number
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.PONumber)%>
            <%: Html.ValidationMessageFor(model => model.PONumber)%>
        </div>

        <div class="editor-label">
            Date received
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.ReceivedDate)%>
            <%: Html.ValidationMessageFor(model => model.ReceivedDate)%>
        </div>

        <div class="editor-label">
            Deadline Date
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.DeadlineDate)%>
            <%: Html.ValidationMessageFor(model => model.DeadlineDate)%>
        </div>

        <div class="editor-label">
            Date sent
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.SentDate)%>
            <%: Html.ValidationMessageFor(model => model.SentDate)%>
        </div>

        <div class="editor-label">
            Is fixed-cost?&nbsp;<input type="checkbox" id="fixed-cost" />
        </div>

        <div id="word-priced-job">
            <div class="editor-label">
                Word count
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.WordCount)%>
                <%: Html.ValidationMessageFor(model => model.WordCount)%>
            </div>

            <div class="editor-label">
                Word cost
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.WordCost)%>
                <%: Html.ValidationMessageFor(model => model.WordCost)%>
            </div>
        </div>

        <div id="fixed-price-job" class="faded">
            <div class="editor-label">
                Fixed cost
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.FixedCost)%>
                <%: Html.ValidationMessageFor(model => model.FixedCost)%>
            </div>
        </div>

        <div class="editor-label">
            Languages
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.LanguageFrom, Model.Languages, "-- Select --") %><%: Html.ValidationMessageFor(model => model.LanguageFrom)%>
            <span>&nbsp;-&nbsp;to&nbsp;-&nbsp;</span>
            <%: Html.DropDownListFor(model => model.LanguageTo, Model.Languages, "-- Select --") %><%: Html.ValidationMessageFor(model => model.LanguageTo)%>
        </div>

        <div class="editor-label">
            Client
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.Client, Model.ClientsList, "-- Select --") %> <%: Html.ActionLink("Create a new client", "Create", "Clients") %>
            <%: Html.ValidationMessageFor(model => model.Client)%>
        </div>

        <div class="editor-label">
            Job file name
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.JobFileName) %>
            <%: Html.ValidationMessageFor(model => model.JobFileName)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.WorkedBy)%>
        </div>
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.WorkedBy, Model.Users) %>
        </div>

        <p>
            <input id="btnSave" type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>
4

2 に答える 2

3

モデルとビューコードを確認せずにこの例外が発生する理由はわかりませんが、次のようにアクションを単純化してみてください。

[HttpPost]
public ActionResult Create(JobCreationModel job)
{
    if (!ModelState.IsValid)
    {
        return View(job);
    }
    _jobRepository.AddJob(job);
    _jobRepository.Save();
    return RedirectToAction("Index");
}
于 2011-04-02T14:54:08.200 に答える
1

私は変わった:

[StringLength(3)]
public int? GroupID { get; set; }

に:

public int? GroupID { get; set; }

GroupID が int だったので、StringLength 属性が問題を引き起こしたのでしょうか。StringLength 属性は文字列に対してのみ有効であるようです。

于 2011-05-31T17:09:43.133 に答える