传递单选按钮值作为提交的参数

时间:2014-08-06 14:39:34

标签: c# asp.net asp.net-mvc-4 razor

我目前有这种方法:

public ActionResult Edit([Bind(Include = 
"description,tags,files,editFiles")] Task mydata, int keyId){

我想从我的editFiles模型中移除Task并将此方法修改为:

public ActionResult Edit([Bind(Include = 
"description,tags,files")] Task mydata, int keyId, string editFiles){

我的radiobutton目前看起来像这样:

@Html.RadioButtonFor(model => model.editFiles, "no change", new { @checked = true }) Do not change files
@Html.RadioButtonFor(model => model.editFiles, "delete") Delete old files

我完成此任务的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

我会为编辑视图创建一个新的viewmodel,这个viewmodel可以从Task类继承。

public class EditTaskVM : Task
{
  public bool IsEdit { set;get; }
  //Other edit related properties as needed.
}

在您的GET操作方法中(对于编辑),返回此新视图模型的实例,我的编辑视图将强烈输入到此新EditTaskVM

并在视图中使用IsEdit属性

@Html.RadioButtonFor(model => model.IsEdit,"nochange", new { @checked = true })No change 
@Html.RadioButtonFor(model => model.IsEdit,"delete") Delete old files

对于 HttpPost 操作方法,

[HttpPost]
public ActionResult Edit(EditTaskVM model)
{
  // Do your stuff here
  //check for model.IsEdit
  // TO DO : Redirect (PRG pattern)
}