Asp.net mvc 4 我正在构建web应用程序在运行时创建的文本框,并将数据保存在数据库asp.net mvc中
这是我写的代码,它工作不好,请检查我的问题尽快回复Asp.net mvc 4 我正在构建web应用程序在运行时创建的文本框,并将数据保存在数据库asp.net mvc中,asp.net-mvc-4,Asp.net Mvc 4,这是我写的代码,它工作不好,请检查我的问题尽快回复 //HomeController.cs using checkinsert.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace checkinsert.Controllers
//HomeController.cs
using checkinsert.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace checkinsert.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
List<Table> ci = new List<Table> {new Table {Id=0,name="",contactno=""} };
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(List<Table> ci)
{
if(ModelState.IsValid){
using(Database1Entities dc= new Database1Entities())
{
foreach(var i in ci)
{
dc.Tables.Add(i);
}
dc.SaveChanges();
ViewBag.Message="Data is Successful inserted";
ModelState.Clear();
ci= new List<Table>{new Table{Id=0,name="",contactno=""}};
}
}
return View(ci);
}
}
}
//HomeController.cs
使用checkinsert.Models;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
命名空间checkinsert.Controllers
{
公共类HomeController:控制器
{
公共行动结果索引()
{
List ci=new List{new Table{Id=0,name=”“,contactno=”“};
返回视图();
}
[HttpPost]
[ValidateAntiForgeryToken]
公共行动结果索引(列表ci)
{
if(ModelState.IsValid){
使用(Database1Entities dc=new Database1Entities())
{
foreach(ci中的var i)
{
dc.表。添加(i);
}
dc.SaveChanges();
ViewBag.Message=“数据插入成功”;
ModelState.Clear();
ci=新列表{新表{Id=0,name=”“,contactno=”“};
}
}
返回视图(ci);
}
}
}
这是Index.cshtml,问题是文本字段没有显示在浏览器中,这意味着当我们运行时,没有显示文本字段
同时检查jquery文件的路径我觉得jquery文件的路径有问题,因为jquery文件没有运行
@model List<checkinsert.Models.Table>
@{
ViewBag.Title = "Insert Bulk Data";
}
<div style="width:720px;padding:5px;background-color:white;">
@using (Html.BeginForm("Index","Home",FormMethod.Get))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
if(ViewBag.Message!=null)
{
<div style="border:1px solid green">
@ViewBag.Message
</div>
}
<div><a href="#" id="AddNew" >Add New</a></div>
<table id="dataTable" border="0" >
<tr>
<th>Contact Name</th>
<th>Contact No</th>
</tr>
@if (Model != null && Model.Count > 0)
{
int j = 0;
foreach (var i in Model)
{
<tr style="border:1px solid black">
<td>@Html.TextBoxFor(a=>a[j].name)</td>
<td>@Html.TextBoxFor(a=>a[j].contactno)</td>
<td>
@if(j>0)
{
<a href="#" class="remove">Remove</a>
}
</td>
</tr>
j++;
}
}
</table>
<input type="submit" value="Save Data" />
}
</div>
@* here is the JQuery code *@
@section Scripts {
@Scripts.Render("checkinsert/jquery-3.0.0.min.js")
<script lang="javascript">
$(document).ready(function () {
$("#addNew").click(function () {
e.preventDefault();
var $tablebody = $("#dataTable");
var $trLast = $tablebody.find("tr:last");
var $trNew = $trLast.clone();
var $suffix = $trNew.find(':input:first').attr('name').match(/\d+/);
$trNew.find("td:last").html('<a href="#" class="remove">Remove</a>');
$.each($trNew.find(':input'), function (i, val) {
// Replaced Name
var $oldN = $(this).attr('name');
var $newN = $oldN.replace('[' + suffix + ']', '[' + (parseInt(suffix) + 1) + ']');
$(this).attr('name',nawN);
//Replaced value
var type = $(this).attr('type');
if (type.toLowerCase == "text")
{
$(this).attr('value', '');
// if you have another type then replaced this "text" with default value
$(this).removeClass("input-validation-error");
}
});
$trLast.after($trNew);
// re-assign validation
var form = $("form")
.removeData("validator")
.removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse(form);
});
// 2.remove
$('a.remove').live("click", function (e) {
e.preventDefault();
$(this).parent().parent().remove();
});
});
</script>
}
@型号列表
@{
ViewBag.Title=“插入批量数据”;
}
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Get))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
if(ViewBag.Message!=null)
{
@查看包。留言
}
联系人姓名
联系电话
@if(Model!=null&&Model.Count>0)
{
int j=0;
foreach(模型中的var i)
{
@Html.TextBoxFor(a=>a[j].name)
@Html.TextBoxFor(a=>a[j].contactno)
@如果(j>0)
{
}
j++;
}
}
}
@*这是JQuery代码*@
@节脚本{
@Scripts.Render(“checkinsert/jquery-3.0.0.min.js”)
$(文档).ready(函数(){
$(“#添加新”)。单击(函数(){
e、 预防默认值();
var$tablebody=$(“#数据表”);
var$trLast=$tablebody.find(“tr:last”);
var$trNew=$trLast.clone();
var$suffix=$trNew.find(':input:first').attr('name').match(/\d+/);
$trNew.find(“td:last”).html(“”);
$.each($trNew.find(':input'),函数(i,val){
//更名
var$oldN=$(this.attr('name');
变量$newN=$oldN.replace('['+后缀+']','['+(parseInt(后缀)+1)+']');
$(this.attr('name',nawN);
//替换值
var type=$(this.attr('type');
if(type.toLowerCase==“text”)
{
$(this.attr('value','');
//如果您有其他类型,则用默认值替换此“文本”
$(this).removeClass(“输入验证错误”);
}
});
$trLast.after($trNew);
//重新分配验证
变量形式=$(“形式”)
.removeData(“验证器”)
.removeData(“不引人注目的验证”);
$.validator.unobtrusive.parse(表单);
});
//2.移除
$('a.remove').live(“单击”),函数(e){
e、 预防默认值();
$(this.parent().parent().remove();
});
});
}
您的代码中有两个问题
public ActionResult Index()
{
List<Table> ci = new List<Table> {new Table {Id=0,name="",contactno=""} };
return View(ci);
}
public ActionResult Index()
{
List ci=new List{new Table{Id=0,name=”“,contactno=”“};
返回视图(ci);
}
@if (Model != null && Model.Count > 0)
{
foreach (var tabel in Model)
{
<tr style="border:1px solid black">
<td>@Html.TextBoxFor(a => tabel.name)</td>
<td>@Html.TextBoxFor(a => tabel.contactno)</td>
<td>
<a href="#" class="remove">Remove</a>
</td>
</tr>
}
}
@if(Model!=null&&Model.Count>0)
{
foreach(模型中的var tabel)
{
@Html.TextBoxFor(a=>tabel.name)
@Html.TextBoxFor(a=>tabel.contactno)
}
}
代码中有两个问题
public ActionResult Index()
{
List<Table> ci = new List<Table> {new Table {Id=0,name="",contactno=""} };
return View(ci);
}
public ActionResult Index()
{
List ci=new List{new Table{Id=0,name=”“,contactno=”“};
返回视图(ci);
}
@if (Model != null && Model.Count > 0)
{
foreach (var tabel in Model)
{
<tr style="border:1px solid black">
<td>@Html.TextBoxFor(a => tabel.name)</td>
<td>@Html.TextBoxFor(a => tabel.contactno)</td>
<td>
<a href="#" class="remove">Remove</a>
</td>
</tr>
}
}
@if(Model!=null&&Model.Count>0)
{
foreach(模型中的var tabel)
{
@Html.TextBoxFor(a=>tabel.name)
@Html.TextBoxFor(a=>tabel.contactno)
}
}
检查答案并测试它,等待你的反馈检查答案并测试它,等待你的反馈我听不懂你的答案请详细解释或发送解决方案你的观点采取模式
列表