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

本文关键字:参数 单选按钮 提交 | 更新日期: 2023-09-27 18:16:37

我现在有这个方法:

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

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

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

我的单选按钮现在看起来是这样的:

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

我应该怎么做?

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

我将为编辑视图创建一个新的视图模型,这个视图模型可以继承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 action方法,

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