>引言
我正在使用EF6和SQL服务器开发ASP.NET MVC项目。
>说明
正如您在下面的代码中看到的,我有一个方法Filter
的控制器,可以根据过滤,分页和排序显示一些文章。
我在表格中显示文章,每个文章都有一个复选框。您可以选中这些框,然后选择2个按钮:Add
和Remove
。
添加:将所选文章设置为有效(db中的字段IsActive = 1
)
删除:将所选文章设置为非活动状态(db中的字段IsActive = 0
)
使用jQuery我根据按钮提交表单,如果需要将所选文章设置为活动或非活动状态,则将参数状态设置为标识。
Al的工作正常,我在SetStateArticles
方法中设置了一个断点,我得到了所选的id和状态。需要编写将其处理到db中的其余代码,但这不是问题。当我使用新网址设置表单时,我丢失了过滤的网址值。我想保留这个,因为我需要根据过滤重新加载页面,这样用户就不需要再次设置过滤,排序和分页。
我该怎么做?如何运行控制器的方法但仍保留在同一页面上或保存url参数的值?
与webforms一样,您只需回发,在点击事件上执行某些操作并返回同一页面。
>代码
控制器的
public class ArticlesController : Controller
{
private ProsecMdmEntities db = new ProsecMdmEntities();
// GET: Articles/Filter
public ActionResult Filter(
string supsf, string supso, int? supp,
string artgsf, string artgso, int? artgp,
string artsf, string artso, int? artp,
int? supplier, int? articleGroup
)
{
// do something.....
}
// POST: /Articles/SetStateArticles
// Set the IsActive to true or false
[HttpPost]
public ActionResult SetStateArticles(int[] items, bool state)
{
return View();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
视图的
<script>
$(document).ready(function () {
// Add items: set IsActive to true
$('.add-items').click(function () {
var $form = $(this).closest('form');
$form.attr('action', '@Url.Action("SetStateArticles", "Articles", new { state = true })');
$form.submit();
});
// Remove items: set IsActive to false
$('.remove-items').click(function () {
var $form = $(this).closest('form');
$form.attr('action', '@Url.Action("SetStateArticles", "Articles", new { state = false })');
$form.submit();
});
});
</script>
答案 0 :(得分:0)
SignalR将是完成这项工作的完美工具!
由于您将使用SignalR,因此不会重新加载页面。
这将消除在页面上尝试和保持用户位置(过滤/排序)的需要,因为维护该代码,我怀疑这将是一个“好”的方法。
您可以执行以下操作:
Hub(将其视为您的控制器)
[HubName("nameOfHub")]
public class NameOfHub: Hub
{
public void SetStateArticles(int[] items, bool state)
{
// All your logic for whatever you want to do, change a flag
Clients.Client(Context.ConnectionId).messageSuccess("What ever you want reported back");
}
查看强>
var hub = $.connection.nameOfHub;
hub.client.messageSuccess= function(message) {
// Notify the user? This is once your SetStateArticles has finished
// and we have called Clients.Client(Context.ConnectionId).messageSuccess in the Hub class
}
$(document).ready(function () {
$.connection.hub.start().done(function () {
console.log("Connected");
$('.add-items').click(function () {
hub.server.SetStateArticles(your params);
});
});
});
如果您不想沿着SignalR路线走,那么我建议Ajax
来电。
查看强>
$.ajax('@Url.Action("SetStateArticles", "Articles", new { state = true })')
.done(function (data) {
// It's now updated so you can inform the user or do something else?
});
答案 1 :(得分:0)
好的,使用这里的评论,我已经解决了这个问题。
您可以在下面找到处理使用我的Web API 2更新文章的发布请求的代码:
<强>的jQuery 强>
setItemState: function (state) {
var values = $('input:checkbox[name=items]:checked').map(function () {
return this.value;
}).get();
$.post(uri_api + '/setstatearticles', { items: values, state: state.toString() })
.done(function (data) {
location.reload(true); // refresh the page after the changes
})
.fail(function (jqXHR, textStatus, err) {
console.log('Error: ' + err);
});
}
Web API
// POST: api/ArticlesApi/SetStateArticles
// Set the IsActive to true or false
[HttpPost]
[ResponseType(typeof(void))]
public IHttpActionResult SetStateArticles(JsArticleItemState jsItem)
{
foreach (string item in jsItem.Items)
{
int id = int.Parse(item);
Article article = db.Articles.Find(id);
article.IsActive = int.Parse(jsItem.State) == 1;
db.Entry(article).State = EntityState.Modified;
}
db.SaveChanges();
return StatusCode(HttpStatusCode.NoContent);
}