質問

I have a simple example. Two class. User and Company like :

public class User() {
    public int UserID { get; set; }

    [Display(Name = "User name")]
    public string Name { get; set; }

    [Display(Name = "Company")]
    public int CompanyID { get; set; }

    public virtual Company Company { get; set; }
}

public class Company() {
    public int CompanyID { get; set; }

    [Display(Name = "Company")]
    public string Name { get; set; }

    public virtual ICollection<User> Users { get; set; }
}

My problem is in the Create and the Edit views of the User. The label for "Name" is displayed correctly in "User name" but the label for "CompanyID" stay displayed at "CompanyID" (the drop down list is created correctly with all Companies). I want the label display "Company" like I make it in the class.

I've try to change my view but all I do block compilation so I'm lost. I'm begginer in MVC so excuse me if it easy to do but I don't see it.

Edit (add Create View code) :

@model Intranet3.Models.User
@{
    ViewBag.Title = "Add a user";
}
@using (Html.BeginForm(null, null, FormMethod.Post, htmlAttributes: new { @class = "form-horizontal form-bordered" })) {
    @Html.AntiForgeryToken()
    <div class="row-fluid">
        <div class="span12">
            <div class="box">
                <div class="box-title">
                    <h3>
                        <i class="icon-table"></i>
                        New
                    </h3>
                </div>
                <div class="box-content nopadding">
                    <div class="form-horizontal">
                        @Html.MyValidationSummary()

                        <div class="control-group @Html.ClassErrorFor(model => model.Name)">
                            @Html.LabelFor(model => model.Name, new { @class = "control-label" })
                            <div class="controls">
                                @Html.EditorFor(model => model.Name)
                                @Html.MyValidationMessageFor(model => model.Name)
                            </div>
                        </div>

                        <div class="control-group @Html.ClassErrorFor(model => model.CompanyID)">
                            @Html.LabelFor(model => model.CompanyID, "CompanyID", new { @class = "control-label" })
                            <div class="controls">
                                @Html.DropDownList("CompanyID", String.Empty)
                                @Html.MyValidationMessageFor(model => model.CompanyID)
                            </div>
                        </div>

                        <div class="form-actions">
                            <button type="submit" class="btn btn-primary">Create</button>
                            <button onclick="location.href='@Url.Action("Index","User")'" type="button" class="btn">Cancel</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
}

Edit 2 : Problem solved by delete the string force in Labels. So this :

@Html.LabelFor(model => model.CompanyID, "CompanyID", new { @class = "control-label" })

Need to be

@Html.LabelFor(model => model.CompanyID, new { @class = "control-label" })
役に立ちましたか?

解決

Why have u passed parameter CompanyId

@Html.LabelFor(model => model.CompanyID, "CompanyID", new { @class = "control-label" })

Should be

@Html.LabelFor(model => model.CompanyID, new { @class = "control-label" })

他のヒント

@Html.TextBoxFor(c => c.CompanyID, new { data_bind = "text:Contacts.FirstName", @class = "form-control" })

If you have knockoutjs binding

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top