asp.net mvc前端post提交json数据给controller,后台参数怎么对应接收
asp.net mvc前端post提交json数据给controller,后台参数如何对应接收?
前端程序
js代码如下:
后台对应的controller
现在的问题是后台M.News str这个参数获取到的参数不全,请大家指点一下!谢谢!!!
------解决思路----------------------
model.news=request[ " 前台要穿过来的参数 "] 这样就传给model了
------解决思路----------------------
用Request.Form["News_Title"]试一试
------解决思路----------------------
1:不用加JSON.stringify
2:js有.trim()的用法吗...我记得应该是$.trim()....
3:你的data:{}里没有把图片传过来....
前端程序
<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这个参数获取到的参数不全,请大家指点一下!谢谢!!!
------解决思路----------------------
model.news=request[ " 前台要穿过来的参数 "] 这样就传给model了
------解决思路----------------------
用Request.Form["News_Title"]试一试
------解决思路----------------------
1:不用加JSON.stringify
2:js有.trim()的用法吗...我记得应该是$.trim()....
3:你的data:{}里没有把图片传过来....