原理: 引用model(@model modelName)的画面,提交到后台的model对象,属性与前台post标签name属性对应来获取值。
前台:
@model myModel
@using(Html.BeginForm()){
<input id=‘xxx‘ name=‘userName‘> </input>
}
后台方法:
[HtmlPost]
public ActionResult Test(UserModel user){
//user.userName id 为xxx的输入框值 【userName相对应】
return @Url.RedirectToAction(‘ActionName‘,‘RotueName‘);
}
实现:
model :
public class UserModel{
public string userName{set;get;}
xxxxxx
public string submitType{set;get;}
}
view:
<script type=‘text/javascript‘>
$(‘#insert‘).live(‘click‘,function(){
$(‘#submit‘).val(‘insert‘);
$(‘#submit‘).trigger(‘click‘);
});
$(‘#delete‘).live(‘click‘,function(){
$(‘#submit‘).val(‘delete‘);
$(‘#submit‘).trigger(‘click‘);
});
</script>
<div>
<button id=‘insert‘ type=‘button‘>提交</button>
<button id=‘delete‘ type=‘button‘>删除</button>
<input id=‘submit‘ name=‘submitType‘ type=‘submit‘ style=‘display:none‘ />
<div>
controll 方法:
public ActionResult UserEdit(UserModel mdl){
if(mdl.submitType=="insert"){
//插入操作
}else if(mdl.submitType=="delete"){
//删除操作
}
}