背景: mvc json实现选择列表之后,发布功能
功能:当选择列表ck之后,用json拼接成一个字符串包含:id与是否选中两列,然后在服务器端进行批量修给数据库数据,最后修改成功之后,给页面提示的功能
从客户端到服务器端用的是json传输,从服务器端传输到客户端哦是json传输。
html:
@model Glodon.Collaboration.WebUI.Models.MainExpertModels
@{
ViewBag.Title = " 专家管理";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="/Scripts/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
<script type="text/javascript">
function GetJson() {
var Jsons = "[";
$('input[id="item_Ispublish"]').each(function () {
var trJson;
var Ispublish = $(this).attr("checked");
var id;
id = $(this).nextAll('input[id="item_id"]').val();
trJson = "'Id':" + "'" + id + "',";
if (Ispublish == "checked") {
Ispublish = true;
}
else if (Ispublish == undefined) {
Ispublish = false;
}
trJson = trJson + "'Ispublish':" + "'" + Ispublish + "'";
trJson = "{" + trJson + "}";
Jsons += trJson + ",";
});
//去掉最后一组数据后面的“,”
if (Jsons.lastIndexOf(",") == Jsons.length - 1)
Jsons = Jsons.substring(0, Jsons.length - 1);
Jsons = Jsons + "]";
return Jsons;
}
function listtext() {
var json = GetJson();
$.ajax({
url: "/Expert/Goto_Increased_Publish",
dataType: "json",
data: "data=" + json,
type: "POST",
success: function (data) {
var arr = eval('(' + data + ')').result;
switch (arr) {
case '1':
alert("发布成功");
return;
default: alert("发布失败"); return;
}
}
});
}
</script>
<h2>
专家管理</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>专家管理</legend>
<table
@if (Model.PageFlag == "Increased" || Model.PageFlag == "Published" || Model.PageFlag == "IncreasedCity" || Model.PageFlag == "PublishedCity")
{
@Html.Raw("style=\"width: 100%;display:none;\"")
}
else
{
@Html.Raw("style=\"width: 100%;\"")
}>
<tr>
<td align="right">
姓名
</td>
<td>@Html.TextBoxFor(m => m.name, new { style = "width:120px" })
</td>
<td align="right">
年龄
</td>
<td>
@Html.TextBoxFor(m => m.age, new { style = "width:120px" })
</td>
<td align="right">
性别
</td>
<td>
@if (Model != null)
{
@Html.DropDownListFor(m => m.sex, new SelectList(Model.SexSource, "Key", "Value", " == 选择类型 == "))
}
</td>
</tr>
<tr>
<td align="right">
专业
</td>
<td>
@Html.TextBoxFor(m => m.speciality, new { style = "width:120px" })
</td>
<td align="right">
机构名称
</td>
<td>
@Html.TextBoxFor(m => m.jigoumingcheng, new { style = "width:120px" })
</td>
</tr>
<tr>
<td align="right">
入库时间
</td>
<td>
<input id="begin" value=@Model.begindate type="text" onClick="WdatePicker()"style=" width:120px"/>至
</td>
<td>
<input id="end" value=@Model.enddate type="text" onClick="WdatePicker()" style=" width:120px"/>
</td>
</tr>
<tr>
<td colspan="6">
<p align="center">
<input type="submit" id="summbitpublish" value="查 询" />
</p>
</td>
</tr>
</table>
<table
@if (Model.PageFlag == "Browser")
{
@Html.Raw("style=\"width: 100%;display:none;\"")
}
else
{
@Html.Raw("style=\"width: 100%;\"")
}>
<tr>
<td>
<a href="#" οnclick="window.open('./Edit?hrId=','','height=600,width=800,fullscreen=no,resizable=yes,scrollbars=no,top=350,left=500')">
添 加</a> <a id="searchsubmit" οnclick="listtext()">发布</a>
</td>
</tr>
</table>
<table cellspacing="0" cellpadding="4" id="GridView1" style="color: #333333; width: 100%;
border-collapse: collapse;">
<tr style="color: White; background-color: #507CD1; font-weight: bold;">
<th style="width: 80; text-align: center" scope="col">
序号
</th>
<th style="width: 80; text-align: center" scope="col">
姓名
</th>
<th style="width: 80; text-align: center" scope="col">
性别
</th>
<th style="width: 80; text-align: center" scope="col">
年龄
</th>
<th style="width: 80; text-align: center" scope="col">
专业
</th>
<th style="width: 80; text-align: center" scope="col">
入库时间
</th>
<th style="width: 80; text-align: center" scope="col">
联系方式
</th>
<th style="width: 80; text-align: center" scope="col">
上报单位
</th>
<th style="width: 80; text-align: center" scope="col">
上报时间
</th>
@if (Model.PageFlag != "Browser" || Model.PageFlag != "Published")
{
<th scope="col" style="width: 40; text-align: center">
发布
</th>
}
@if (Model.PageFlag != "Browser")
{
<th scope="col" style="width: 40; text-align: center">
操作
</th>
}
</tr>
@if (Model.ExpertModelsList != null)
{
int i = 0;
foreach (var item in Model.ExpertModelsList)
{
<tr
@if (i++ % 2 == 1)
{
@Html.Raw("style=\"background-color: #EFF3FB;\"")
}
else
{
@Html.Raw("style=\"background-color: White;\"")
}>
<td>
@Html.Hidden("item.HrId", item.HrId, new { id = "item.HrId" })
@Html.DisplayFor(modelItem => item.xuhao)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.SexStr)
</td>
<td>
@Html.DisplayFor(modelItem => item.age)
</td>
<td>
@Html.DisplayFor(modelItem => item.speciality)
</td>
<td>
@(item.Birthday == null ? "" : item.CreateTime.Value.ToShortDateString())
</td>
<td>
@Html.DisplayFor(modelItem => item.phone)
</td>
<td>
@Html.DisplayFor(modelItem => item.JigouId)
</td>
<td>
@(item.Birthday == null ? "" : item.PublishTime.Value.ToShortDateString())
</td>
@if (Model.PageFlag != "Browser" && Model.PageFlag != "Published")
{
<td>
@Html.CheckBox("item.Ispublish", item.Ispublish)
@Html.Hidden("item.id", item.HrId)
</td>
}
@if (Model.PageFlag != "Browser")
{
<td>
<a href="#" οnclick="window.open('./Edit?hrId=@item.HrId','','height=600,width=800,fullscreen=no,resizable=yes,scrollbars=yes,top=350,left=500')">
编辑</a>| <a href="#" οnclick="window.open('./Detail?hrId=@item.HrId','','height=600,width=800,fullscreen=no,resizable=yes,scrollbars=yes,top=350,left=500')">
详情</a>
@if (Model.PageFlag == "Published")
{
<a href="Has_Published_Delete?id=@item.HrId" οnclick='return confirm("确定要删除吗?")'>|删除</a>
}
else
{
<a href="Goto_Increased_Delete?id=@item.HrId"οnclick='return confirm("确定要删除吗?")'>|删除</a>
}
</td>
}
</tr>
}
<tr>
<td colspan="11">
<div class="Pagination">@Glodon.Collaboration.WebUI.HtmlExtension.Pagination(this.Html, (int)ViewBag.PageSize, (int)ViewBag.Index,
(int)ViewBag.RecordCount, new { @ViewType = 0, @Identifier = "page", @Encrypt = false })</div>
</td>
</tr>
}
</table>
</fieldset>
}
control:
[HttpPost]
public JsonResult Goto_Increased_Publish(string data)
{
JavaScriptSerializer js = new JavaScriptSerializer();
List<PublishModel> list = js.Deserialize<List<PublishModel>>(data);
foreach (var s in list)
{
var id = s.Id;
var isPublish = s.Ispublish;
var expert = _repo.Get<TabHr>(id);
expert.Ispublish = isPublish;
_repo.Save();
}
string Data = "{'result':'1'}";
return Json(Data);
}