在按钮上单击添加一行并将插入的数据保存在数据库中 [英] Adding a row on button click and saving the inserted data in the database

查看:92
本文介绍了在按钮上单击添加一行并将插入的数据保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我尝试将我的两个当前视图集成在一起,但它不起作用。



这是我查看表格的视图。这是我希望'+'符号添加新值的地方。



@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项目){

< 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(编辑,编辑,新{id = item.TCSetID})|

@ Html.ActionLink(详细信息,详细信息,新{id =项目。 TCSetID})|

@ Html.ActionLink(删除,删除,新{id = item.TCSetID})

< / td>

< / tr>

}

< / table>

@ Html.ActionLink(Create New, 创建,新{id = ViewBag.id})





在`Html.ActionLink()`我想通过父表的id。我的create函数将父id作为参数。但ViewBag.id也无济于事。



我的任务是什么工作?



编辑1



添加创建视图



I want to add a '+' symbol below the table that shows the content of a particular table enabling the user to add another row of data to the table. I want the data to be saved in the database.

I tried integrating my two current views together but it doesn't work.

Here is my View for viewing the table. Here is the place where I want the '+' Symbol to add new values.

@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 })


In the `Html.ActionLink()` I want to pass the id of the parent table. My create function takes parent id as a parameter. But ViewBag.id doesn't help as well.

What is the work around for my task?

EDIT 1

Addition of the Create View

       @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")
    }


EDIT 2 : Adding Create Action method


<pre lang="c#">        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();
        }







编辑3 - 添加模型类






EDIT 3 - Adding model classes

   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; }
}





**编辑4 **:针对特定TCID的ViewTCSet







**EDIT 4**: ViewTCSet for a particular TCID


  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);
}



查看ViewTCSet





@model TCImplementation.ViewModels.TC_TCSet

@ {

ViewBag.Title =ViewTCSet;

}

< table>

< tr>

< th> Tc设置名称< / th>

< th>数据用法< ; / th>

< th>数据状态< / th>

< th>数据格式< / th>



< / tr>

@foreach(Model.TCSet中的var项目)

{

< tr>

< td> @ item.ValueName< / td>

< td> @ item.DataUsage< / td>

< td> @ item.DataStatus< / td>

< td> @ item.DataFormat.FormatName< / td>

< td> @ Html.ActionLink(编辑,编辑,TCSets,新{id = item.TCSetID},null)| @ Html.ActionLink(详细信息,详细信息,TCSets,新{id = item.TCSetID},null)| @ Html.ActionLink(删除,删除,TCSets,新{id = item.TCSetID},null)< / td>

< / tr>

}

< / table>

@ Html.ActionLink(创建,创建,TCSets,新{id = Model.TCSet },null)





在这个actionlink中我无法传递值`Model.TCSet.TCID`


View for ViewTCSet


@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)


In this actionlink I am unable to pass the value `Model.TCSet.TCID`

推荐答案

第一个apporach:

您在此处共享的视图显示了数据库中的所有记录。如果要再添加一行,这意味着您正在创建新记录。为此,你需要一个不同的视图。 (你已经删除了)



第二种方法:

你可以通过jQuery来实现。请参阅:使用ASP中的jQuery动态添加/删除行.NET [ ^ ]。

您在那里添加了一个表单并将其标记为HTML表格行,并附加到表格中。

然后调用HttpPost Action Method来保存这些数据。



-KR
1st apporach:
The View you have shared here is showing all the records from database. If you want to add one more row which means that you are creating a new record. For that you'd be requiring a different View. (Which you removed already)

2nd approach:
You can do it via jQuery. See this: Add / Delete Rows Dynamically using jQuery in ASP.NET[^].
You have add a form over there and markup it as HTML table row, and append to the table.
Then call the HttpPost Action Method to save this data.

-KR


这篇关于在按钮上单击添加一行并将插入的数据保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆