在按钮上单击添加一行并将插入的数据保存在数据库中

时间:2015-09-18 07:50:47

标签: c# html mysql asp.net-mvc razor

我想添加' +'表格下方的符号,显示特定表格的内容,使用户能够向表格添加另一行数据。我希望将数据保存在数据库中。

我尝试将我目前的两个观点整合在一起,但它并不起作用。

这是我查看表格的视图。这是我想要' +'的地方。用于添加新值的符号。

use date_default_timezone_set('Europe/Istanbul');

@model IEnumerable<TCImplementation.Models.TCSet> <table class="table"> <tr> <th>@Html.DisplayNameFor(model => model.ValueName)</th> <th>@Html.DisplayNameFor(model => model.DataFormat.FormatName)</th> <th>@Html.DisplayNameFor(model => model.TC.TCName)</th> <th>@Html.DisplayNameFor(model => model.DataUsage)</th> <th>@Html.DisplayNameFor(model => model.DataStatus)</th> <th></th> </tr> @foreach (var item in Model) { <tr> <td>@Html.DisplayFor(modelItem => item.ValueName)</td> <td>@Html.DisplayFor(modelItem => item.DataUsage)</td> <td>@Html.DisplayFor(modelItem => item.TC.TCName)</td> <td>@Html.DisplayFor(modelItem => item.DataFormat.FormatName)</td> <td>@Html.DisplayFor(modelItem => item.DataStatus)</td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.TCSetID }) | @Html.ActionLink("Details", "Details", new { id=item.TCSetID }) | @Html.ActionLink("Delete", "Delete", new { id=item.TCSetID }) </td> </tr> } </table> @Html.ActionLink("Create New", "Create", new {id = ViewBag.id }) 我想传递父表的id。我的create函数将父id作为参数。但ViewBag.id也没有帮助。

我的任务是什么工作?

编辑1

添加创建视图

Html.ActionLink()

编辑2:添加创建操作方法

@model TCImplementation.Models.TCSet
@using (Html.BeginForm()) 
{
  @Html.AntiForgeryToken()
  @Html.ValidationSummary(true, "", new { @class = "text-danger" })

  @Html.LabelFor(model => model.ValueName, htmlAttributes: new { @class = "control-label col-md-2" })
  @Html.EditorFor(model => model.ValueName, new { htmlAttributes = new { @class = "form-control" } })
  @Html.ValidationMessageFor(model => model.ValueName, "", new { @class = "text-danger" })

  @Html.LabelFor(model => model.DataUsage, htmlAttributes: new { @class = "control-label col-md-2" })
  @Html.EnumDropDownListFor(model => model.DataUsage, htmlAttributes: new { @class = "form-control" })
  @Html.ValidationMessageFor(model => model.DataUsage, "", new { @class = "text-danger" })

  @Html.LabelFor(model => model.DataStatus, htmlAttributes: new { @class = "control-label col-md-2" })
  @Html.EnumDropDownListFor(model => model.DataStatus, htmlAttributes: new { @class = "form-control" })
  @Html.ValidationMessageFor(model => model.DataStatus, "", new { @class = "text-danger" })

  @Html.LabelFor(model => model.TCID, "TCID", htmlAttributes: new { @class = "control-label col-md-2" })
  @Html.DropDownList("TCID", null, htmlAttributes: new { @class = "form-control" })
  @Html.ValidationMessageFor(model => model.TCID, "", new { @class = "text-danger" })

  @Html.LabelFor(model => model.DataFormatID, "DataFormatID", htmlAttributes: new { @class = "control-label col-md-2" })
  @Html.DropDownList("DataFormatID", null, htmlAttributes: new { @class = "form-control" })
  @Html.ValidationMessageFor(model => model.DataFormatID, "", new { @class = "text-danger" })

  <input type="submit" value="Create" class="btn btn-default" />
}
@Html.ActionLink("Back to List", "Index")

@section Scripts {
  @Scripts.Render("~/bundles/jqueryval")
}

编辑3 - 添加模型类

    public ActionResult Create(int id)
    {
        ViewBag.id = id;
        ViewBag.DataFormatID = new SelectList(db.DataFormat, "DataFormatID", "FormatName");
        ViewBag.TCID = new SelectList(db.TC, "TCID", "TCName",id);
        return View();
    }

编辑4 :针对特定TCID的ViewTCSet

public class TC
{
    public int TCID { get; set; }
    public string TCName { get; set; }
    public virtual ICollection<TCSet> TCSets { get; set; }

}



public class TCSet
{
    public int TCSetID { get; set; }
    public string ValueName { get; set; }
  //  public string DataFormat { get; set; }
    public DataUsage DataUsage { get; set; }
    public DataStatus DataStatus { get; set; }
    public int TCID { get; set; }
    public int DataFormatID { get; set; }
    public virtual TC TC { get; set; }
    public virtual DataFormat DataFormat { get; set; }
}

查看ViewTCSet

    public ActionResult ViewTCSet(int ?id)
    {
        var viewmodel = new TC_TCSet();

        if(id!=null)
        {
            ViewBag.TCID = id.Value;
            var tcSet = db.TC.Include(x => x.TCSets).FirstOrDefault(x => x.TCID == id);
            if(tcSet!=null)
            {
                viewmodel.TCSet = tcSet.TCSets;
            }
        }
        return View(viewmodel);
    }

在此actionlink中,我无法传递值@model TCImplementation.ViewModels.TC_TCSet @{ ViewBag.Title = "ViewTCSet"; } <table> <tr> <th>Tc Set Name</th> <th>Data Usage</th> <th>Data Status</th> <th>Data Format</th> </tr> @foreach(var item in Model.TCSet) { <tr> <td>@item.ValueName</td> <td>@item.DataUsage</td> <td>@item.DataStatus</td> <td>@item.DataFormat.FormatName</td> <td>@Html.ActionLink("Edit", "Edit", "TCSets", new { id= item.TCSetID},null) | @Html.ActionLink("Details", "Details", "TCSets", new { id = item.TCSetID }, null) | @Html.ActionLink("Delete", "Delete", "TCSets", new { id = item.TCSetID }, null)</td> </tr> } </table> @Html.ActionLink("Create", "Create", "TCSets", new { id = Model.TCSet }, null)

0 个答案:

没有答案