asp.net mvc前端post提交json数据给controller,后台参数如何对应接收?

li5206610 2014-12-09 05:17:16
前端程序

<div class="right_body">
<div class="body">
<div class="top_subnav">CMS内容管理平台 > 首页 > 发布文章</div>
<div class="title_h3">
发布文章
</div>
<p class="line" style="margin-top:0;">
</p>
<div class="news">
<div>
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "newsinsertform" }))
{
<p><span>文章标题:</span>@Html.TextBoxFor(a => a.News_Title, new { @class = "newstitle" })</p>
<p><span>文章类别:</span>@Html.DropDownListFor(a => a.NewsTypes.Id, (SelectList)ViewData["NewsType"], new { @class = "newstype" })</p>
<p><span>图片新闻:</span>@Html.CheckBoxFor(a => a.IsPic, new { @class = "IsPic" })<input type="file" id="fileupload" name="fileupload" class="upload" /></p>
<p><span>文章内容:</span></p>
<hr />
<div class="htmleditor">@Html.TextAreaFor(a => a.News_Content, new { @class = "newscontent" })</div>
}
</div>
</div>
<p class="line" style="margin-top:0;">
</p>
<div class="f">
<img src="~/Content/themes/manage/images/submit.gif" id="sub" />
</div>
<div id="element_to_pop_up">
<div class="message">恭喜你,提交成功了!</div>
</div>
</div>
</div>

js代码如下:

$("#sub").click(function () {
if ($(".newstitle").val() != "" && $(".newstitle").val() != "标题不能为空") {
$("#newsinsertform").ajaxSubmit({
type: "post",
dataType: "json",
url: "/Manage/NewsInsert",
data: JSON.stringify({ News_Title: $(".newstitle").val().trim(), News_Content: editor.document.getBody().getHtml(), News_Writer: "admin", IsPic: $(".IsPic").val().trim(), News_TypeId: $(".newstype").val() }),
success: function () {//提示提交成功
$("#element_to_pop_up").bPopup({
autoClose: true,
speed: 1500
});
},
error: function (xmlHttpRqquest, textStatus, errorThrown) {
alert(errorThrown);
}
});

} else {
$(".newstitle").Text("<span style='color:red'>*标题不能为空<span>");
}
});


后台对应的controller

/// <summary>
/// 发布页面()
/// </summary>
/// <returns></returns>
public ActionResult NewsInsert()
{
var b = new NewsTypes();
var newstypelist = b.GetList();
var selectlist = new SelectList(newstypelist, "Id", "TypeName");
ViewData["NewsType"] = selectlist;
return View();
}

/// <summary>
/// 发布页面
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
[HttpPost]
public ActionResult NewsInsert(M.News str)
{
if (!ModelState.IsValid)
{
return View("NewsInsert", str);
}
HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
if (hfc.Count > 0)
{
String imgPath = "";
string filename = Guid.NewGuid() + "." + hfc[0].FileName.Substring(hfc[0].FileName.LastIndexOf(".", StringComparison.Ordinal) + 1);
imgPath = "/images/" + filename;
String physicalPath = Server.MapPath(imgPath);
hfc[0].SaveAs(physicalPath);
str.News_Picture = filename;
}
_bn.Insert(str);
return Json(str, JsonRequestBehavior.AllowGet);
}


现在的问题是后台M.News str这个参数获取到的参数不全,请大家指点一下!谢谢!!!
...全文
27077 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
駡_大_鵬 2015-05-08
  • 打赏
  • 举报
回复



荧光笔标记的部分是关键!
li5206610 2014-12-14
  • 打赏
  • 举报
回复
前面的参数我都写明了的,可是后台接收的时候为啥会有的有,有的就没有
ayanamireizero 2014-12-13
  • 打赏
  • 举报
回复
1:不用加JSON.stringify 2:js有.trim()的用法吗...我记得应该是$.trim().... 3:你的data:{}里没有把图片传过来....
静静-风 2014-12-12
  • 打赏
  • 举报
回复
用Request.Form["News_Title"]试一试
scboy2012 2014-12-12
  • 打赏
  • 举报
回复
model.news=request[ " 前台要穿过来的参数 "] 这样就传给model了
li5206610 2014-12-10
  • 打赏
  • 举报
回复

这个是我调试的
li5206610 2014-12-09
  • 打赏
  • 举报
回复
补充一下Model,请问一定要全部字段补全么???

using System.ComponentModel.DataAnnotations;

namespace M
{
    public class News
    {
        [Key]
        [Display(Name = "ID")]
        public int News_Id { get; set; }

        [Display(Name = "文章标题")]
        public string News_Title { get; set; }

        [Display(Name = "发布时间")]
        public string News_Time { get; set; }

        [Display(Name = "文章作者")]
        public string News_Writer { get; set; }

        [Display(Name = "是否有图片")]
        public bool IsPic { get; set; }

        [Display(Name = "图片名称")]
        public string News_Picture { get; set; }

        [Display(Name = "文章内容")]
        public string News_Content { get; set; }

        [Display(Name = "文章分类")]
        public int News_TypeId { get; set; }

        public virtual NewsTypes NewsTypes { get; set; }
    }
}
xdashewan 2014-12-09
  • 打赏
  • 举报
回复
M.News明显是自定义类,按照json把字段补全不就好了

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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