问题遇到的现象和发生背景
System.NullReferenceException
HResult=0x80004003
Message=未将对象引用设置到对象的实例。
Source=NewsSystem
StackTrace:
id和desc可以传过去
用代码块功能插入代码,请勿粘贴截图
视图中的代码
<script src="~/layui/layui.js"></script>
<script src="~/Js/jquery-3.4.1.min.js"></script>
<h2>编辑新闻</h2>
<div class="layui-form">
<input type="hidden" id="id" value="@Model.id" />
<div class="layui-form-item">
<label class="layui-form-label">标题</label>
<div class="layui-input-block">
<input type="text" name="title" id=" title" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input" value="@Model.Title" />
</div>
</div>
<div class="layui-form-item layui-form-text">
<label class="layui-form-label">内容</label>
<div class="layui-input-block">
<textarea name="desc" id="desc" placeholder="请输入内容" class="layui-textarea" >@Model.Text</textarea>
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="formDemo" id="btn">立即提交</button>
</div>
</div>
</div>
<script>
$(function () {
$("#btn").click(function () {
$.ajax({
url: '/News/EditNewsAction',
type: 'post',
data: {
'id': $('#id').val(),
'title': $('#title').val(),
'desc': $('#desc').val(),
},
success: function (msg) {
if (msg == '修改成功') {
location.href = '/News/Index';
}
else {
layer.open({
title: '警告',
content: '标题或者内容为空'
});
}
},
})
})
})
</script>
public int Update(News t)
{
if (t == null) return 0;
var model = db.News.ToList().FirstOrDefault(item => item.id == t.id);
if (model == null) return 0;
model.Title = t.Title;
model.Text = t.Text;
int count = db.SaveChanges();
return count;
}
实体类
public partial class News
{
public int id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public System.DateTime InsertDate { get; set; }
public int MemberId { get; set; }
public string MemberName { get; set; }
}
数据库的设计
运行结果及报错内容
System.NullReferenceException:“未将对象引用设置到对象的实例。”
我想要达到的结果
解决title传值的原因