Razor 从下拉菜单中插入包含其他表数据的行

Razor 从下拉菜单中插入包含其他表数据的行,razor,asp.net-mvc-4,Razor,Asp.net Mvc 4,我有两个表,我尝试的是在最后一个div中,CategoryName是一个DrobDown列表,其中包含我已经插入的名称…如何获取这些数据?如果代码有什么不清楚的地方,我会解释 这是我的模型: public class Profits { [Key] public int id { get; set; } public double Value { get; set; } public string Description { get; set; } pu

我有两个表,我尝试的是在最后一个div中,CategoryName是一个DrobDown列表,其中包含我已经插入的名称…如何获取这些数据?如果代码有什么不清楚的地方,我会解释 这是我的模型:

public class Profits
{
 [Key]
    public int id { get; set; }    
    public double Value { get; set; }
    public string Description { get; set; }
    public DateTime DateCreation { get; set; }
    public ProfitCategories CategoryName { get; set; }
}
}

这是我的控制器:

 public ActionResult DoInsertProf(Profits profits)
        {
            var Profit = new Profits
            {
                CategoryName = profits.CategoryName,
                Value = profits.Value,
              DateCreation = DateTime.Now,
                Description = profits.Description,
            };
            db.Profit.Add(profits);
            db.SaveChanges();
            return Redirect("/SelectData/ReadProfit");
        }
这是我的观点:

@using (Html.BeginForm("DoInsertProf","InsertProfit"))
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Profits</legend>

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

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




         <div class="editor-label">
            @Html.LabelFor(model => model.CategoryName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CategoryName)
            @Html.ValidationMessageFor(model => model.CategoryName)
        </div>
使用(Html.BeginForm(“DoInsertProf”、“insertprof”)) { @Html.ValidationSummary(true) 利润 @LabelFor(model=>model.Value) @EditorFor(model=>model.Value) @Html.ValidationMessageFor(model=>model.Value) @LabelFor(model=>model.Description) @EditorFor(model=>model.Description) @Html.ValidationMessageFor(model=>model.Description) @LabelFor(model=>model.CategoryName) @EditorFor(model=>model.CategoryName) @Html.ValidationMessageFor(model=>model.CategoryName)
您的问题真的不清楚,您编写的代码也没有帮助…您是否尝试加载已填充下拉列表的表单,或在执行保存后显示一些结果?

尝试加载下拉列表,下拉列表中的数据来自模型:public ProfitCategories CategoryName{get;set;}嗯,好吧,那么你应该使用DropDownList或者DropDownList,也许吧。看看这个