新手请教,MVC中修改后点击保存值没传到。

PLATINUM_II 2017-04-13 04:18:54
Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CompleteTEST.Models;
using TESTBll.Bll;
using TESTBll.EFModel;


namespace CompleteTEST.Controllers
{
public class HomeController : Controller
{
Newtable bll = new Newtable();

// GET: Home
public ActionResult Index()
{
NewModel model = new NewModel();
model.valueList = bll.GetList();
return View(model);
}

// GET: Home/Details/5
public ActionResult Details(string id)
{
return View();
}

// GET: Home/Create
public ActionResult Create()
{
return View();
}

// POST: Home/Create
[HttpPost]
public ActionResult Create(NewModel model, FormCollection collection)
{
try
{
// TODO: Add insert logic here
bll.Add(model.newvalue);
return View(model);
}
catch
{
return View();
}
}

// GET: Home/Edit/5

public ActionResult Edit(string id)
{
NewModel model = new NewModel();
model.newvalue = bll.GetModel(id);
return View(model);
}

// POST: Home/Edit/5
[HttpPost]
public ActionResult Edit(NewModel model, FormCollection collection)
{
try
{
// TODO: Add update logic here
bll.updata(model.newvalue);
return RedirectToAction("Index");
}
catch
{
return View();
}
}

// GET: Home/Delete/5
public ActionResult Delete(string id)
{
return View();
}

// POST: Home/Delete/5
[HttpPost]
public ActionResult Delete(string id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
bll.Delete(id);
return Content("删除成功");
}
catch (Exception ex)
{
return Content(ex.Message.ToString());
}
}
}
}



Models:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TESTBll.EFModel;

namespace CompleteTEST.Models
{
public class NewModel
{
public NEWTABLE newvalue { get; set; }
public List<NEWTABLE> valueList { get; set; }
}
}


Views:
1.Index:
@model CompleteTEST.Models.NewModel
@{
Layout = null;
}

<!DOCTYPE html>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<div style="width:100%; height:30px; text-align:right;">
<a href="Home/Create"target="_blank">新增</a>
</div>
<table border='1' cellspacing="0" cellpadding="0">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>职业</th>
<th>【操作】</th>
</tr>
@foreach (TESTBll.EFModel.NEWTABLE S in Model.valueList)
{
<tr>
<td>@S.ID</td>
<td>@S.NAME</td>
<td>@S.AGE</td>
<td>@S.JOB</td>
<td style="width:120px;">
<a href="Home/Edit/@S.ID" target="_blank" title="@S.ID" id="_updatebtn" style="text-decoration:none">    修改</a>
<a href="#" title="删除" onclick="del_data('@S.ID')" id="_delbtn">删除</a>
</td>
</tr>
}
</table>
</div>
</body>
</html>

<script>
function del_data(val) {
var conf = confirm("确定删除吗?");
if (conf) {
var postData = {
id: val
};
$.ajax({
cache: false,
type: "POST",
url: "@(Url.Action("Delete","Home"))",
data: postData,
success: function (data) {
alert(data);
location.reload();
},
error: function (xhr, ajaxOptions, throwError) {
alert(throwError);
},
traditional: true
});
return false;
}
}
</script>


Edit:


@model CompleteTEST.Models.NewModel
@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>修改</title>
<style>
.txt-box {
width: 250px;
height: 30px;
line-height: 30px;
border: 1px solid red;
border-radius: 5px;
}
</style>
</head>
<body>
<div>
<form method="post" action="/Home/Edit">
<p>
姓名:
@Html.TextBoxFor(model => model.newvalue.NAME, new { @class = "txt-box" });
</p>
<p>
年龄:
@Html.TextBoxFor(model => model.newvalue.AGE, new { @class = "txt-box" });
</p>
<p>
职业:
@Html.TextBoxFor(model => model.newvalue.JOB, new { @class = "txt-box" });
</p>
<p>
<input type="submit" value="保存" />
</p>
</form>
</div>
</body>
</html>


Index:

@model CompleteTEST.Models.NewModel
@{
Layout = null;
}

<!DOCTYPE html>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<div style="width:100%; height:30px; text-align:right;">
<a href="Home/Create"target="_blank">新增</a>
</div>
<table border='1' cellspacing="0" cellpadding="0">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>职业</th>
<th>【操作】</th>
</tr>
@foreach (TESTBll.EFModel.NEWTABLE S in Model.valueList)
{
<tr>
<td>@S.ID</td>
<td>@S.NAME</td>
<td>@S.AGE</td>
<td>@S.JOB</td>
<td style="width:120px;">
<a href="Home/Edit/@S.ID" target="_blank" title="@S.ID" id="_updatebtn" style="text-decoration:none">    修改</a>
<a href="#" title="删除" onclick="del_data('@S.ID')" id="_delbtn">删除</a>
</td>
</tr>
}
</table>
</div>
</body>
</html>

<script>
function del_data(val) {
var conf = confirm("确定删除吗?");
if (conf) {
var postData = {
id: val
};
$.ajax({
cache: false,
type: "POST",
url: "@(Url.Action("Delete","Home"))",
data: postData,
success: function (data) {
alert(data);
location.reload();
},
error: function (xhr, ajaxOptions, throwError) {
alert(throwError);
},
traditional: true
});
return false;
}
}
</script>



Bll:


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TESTBll.EFModel;

namespace TESTBll.Bll
{
public class Newtable
{
EFEntities ctx = new EFEntities();
public List<NEWTABLE> GetList()
{
return ctx.NEWTABLE.ToList<NEWTABLE>();
}
public NEWTABLE GetModel(string id)
{
return ctx.NEWTABLE.Where(b => b.ID == id).FirstOrDefault();
}
public void Add(NEWTABLE newvalue)
{
ctx.NEWTABLE.Add(newvalue);
ctx.SaveChanges();
}
public void updata(NEWTABLE newvalue)
{

ctx.Set<NEWTABLE>().Attach(newvalue);
ctx.Entry(newvalue).State = EntityState.Modified;
ctx.SaveChanges();
}

public void Delete(string id)
{
string sql = "delete NEWTABLE where id = " + id + "";
ctx.Database.ExecuteSqlCommand(sql);
}
}
}


点击保存没反应,设置断点进Edit中的try,到了updata第一句进Edit中try就退出进catch了。我觉得是这个方法有问题。这是照着别人的例子做的,updata方法我不会写,不知道怎么接收用户输入的值,麻烦帮我更正。
...全文
140 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
PLATINUM_II 2017-04-13
  • 打赏
  • 举报
回复
谢谢,我已经解决。是因为没有传主键的值。
正怒月神 2017-04-13
  • 打赏
  • 举报
回复
正怒月神 2017-04-13
  • 打赏
  • 举报
回复
ActionResult 是跳转试图 你使用 ajax进行修改的话,应该是 JsonResult,然后return Json(返回的对象);
PLATINUM_II 2017-04-13
  • 打赏
  • 举报
回复
写重复了,Index只看第一个就行。

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧