【问题标题】:Insert Row from dropdown menu with data from other table使用其他表中的数据从下拉菜单中插入行
【发布时间】:2013-03-01 19:15:59
【问题描述】:

我有 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>

【问题讨论】:

    标签: razor asp.net-mvc-4


    【解决方案1】:

    您的问题确实不清楚,您的代码也无济于事...您是要加载填充了 DropDown 的表单还是在执行保存后显示一些结果?

    【讨论】:

    • 尝试使用下拉列表加载,下拉列表中的数据来自模型中的此处:public ProfitCategories CategoryName { get;放; }
    • 嗯好吧,那么你应该使用 DropDownList 或 DropDownListFor 可能。看看这个question
    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    相关资源
    最近更新 更多