$('#Form')。submit vs $ .post

时间:2017-11-21 14:26:21

标签: javascript jquery asp.net

是否可以做

1) $('#form').submit();

但是这样

2) $.post('/controller/function',formData,function(result){
// I want here to do the same as $('#form').submit();
});

解释当我做1)在返回时,它将使用与我在服务器端返回的视图相关的模型填充我的所有输入

return View(model);

此外,如果我通过在服务器端指定视图来返回不相关的视图和模型,它将使用模型加载视图。

return View(view,model);

我喜欢这种行为,但它不好的一面,它总是在服务器端调用相同的功能

我想要的是我希望能够在服务器端调用不同的功能并保持与我在调用1时发生的行为相同的行为。

2)我可以调用我想要的功能,但我不知道如何使返回动作与1)相同(自动填充我的输入或返回重定向良好视图与加载的模型(填充输入)与模型数据))

服务器端(如果可以提供帮助)

//Use when I perform some search on my page or few actions
//Called with 1) 
[HttpPost]
public ActionResult defaultFunction(Model model)
{
 //Doing some code
 return View(model);
}
//Use on button click I want to switch page to perform some action
//Return the view with the model all ready for the action to be 
//perform by the user
//Called with 2) but not doing what I want
// If I move the code in the defaultFunction and called it with 1) it do 
//what I want : redirect the browser to the view with the model ... 
[HttpPost]
public ActionResult redirectFuntion(Model model)
{
 //Doing some code
 return View("View",model);
}

谢谢 抱歉英语不是我的第一语言。

1 个答案:

答案 0 :(得分:1)

感谢@kblok

根据他的评论,我为我的解决方案做了这个:

$('#form').attr('action','/controller/function').submit();
// it doing the same as if i did $('form').submit(); but I can specified the
// called function on the server side.

它有效!

相关问题