从下拉菜单中插入行与来自其他表的数据

时间:2013-03-01 19:15:59

标签: razor asp.net-mvc-4

我有2个表我尝试的是在最后一个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>

1 个答案:

答案 0 :(得分:0)

您的问题真的不明确,而且您的代码无效......您是否尝试加载填充了DropDown的表单或在执行保存后显示某些结果?