1

我有一个元素列表,我想编辑它。

我创建了这个片段:

@{
    ViewBag.Title = "Liste des comptes administrateurs";

    Layout = "~/Views/Shared/_Layout.cshtml";
    int i = 0;
}

<hgroup class="title">
    <h1 style="color: darkcyan">@ViewBag.Title</h1>
</hgroup>

<section>
    <form>
        <table>
            <tr>
                <td><input type="radio" name="color" id="val" value="red" onclick="@i = 1;">Red</td>
            </tr>
            <tr>
                <td><input type="radio" name="color" value="blue" onclick="@i = 2;">Blue</td>
            </tr>
        </table>
    </form>     
</section>
<section style="margin-top: 30px">        
    <a type="button" href="@Url.Action( "Delete", "Super", new { Id = @i } )">Supprimer</a>
    <a type="button" href="@Url.Action( "Edit", "Super", new { Id = @i } )">Editer</a>
    <br />
    <br />
    <a href="@Url.Action( "Admin_Creation", "Super" )" style="color: blue; margin-left: 150px">Créer un nouveau compte</a>           
</section>

我的问题是,当我单击编辑按钮时,i即使我检查了其中一个单选按钮,其值也是 0。它的值没有改变,始终为 0。

  • 为什么价值i没有改变?
  • 我该如何解决这个错误?
4

3 回答 3

2

您可以使用 FormCollection 对象来完成。

public ActionResult SomeActionMethod(FormCollection formCollection)
{
  foreach (var key in formCollection.AllKeys)
  {
    var value = formCollection[key];
  }
// or 
var color=formCollection["color"];
}
于 2013-05-17T15:26:59.003 回答
1

您应该使用如下代码:

模型:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace MvcDemo.Models
{
public class Color
{

        public int ID { get; set; }
        public Boolean Red { get; set; }
        public Boolean Blue { get; set; }


}
public class ColorContext : DbContext
{
    public DbSet<Color> Colors
    {
        get;
        set;

    }


}
}

看法:

@model MvcDemo.Models.Color

<form action="" method="post" enctype="multipart/form-data">


<table >
<tr>
<td>
  @Html.RadioButtonFor(model=>model.Red,false);

</td>
</tr>
<tr>
<td>
  @Html.RadioButtonFor(model=>model.Blue,false);
</td>
</tr>
</table>
<input type="submit" name="btn" value="update" />
</form>

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcDemo.Models;

namespace MvcDemo.Controllers
{
public class ColorsController : Controller
{
    //
    // GET: /Colors/
    ColorContext cc = new ColorContext();
    public ActionResult Index()
    {
        return View();
    }
    [HttpPost, ActionName("update")]
    public ActionResult Update(int id,Boolean blue,Boolean red)
    {
        Color color = cc.Colors.Find(id);
        color.Blue = blue;
        color.Red = red;
        cc.SaveChanges();
        return RedirectToAction("Index");
    }

}
}    
于 2013-05-17T16:22:49.473 回答
0

使用此代码:

@{
if(IsPost){
    int i;
    if(Request.Form["color"].Equals("red"))
      {
        i = 1;
      }
    else
      {
        i = 2; 
      }
   }
}

<hgroup class="title">
<h1 style="color:darkcyan">@ViewBag.Title</h1>
</hgroup>
<form action="" method="post" enctype="multipart/form-data">
 <table >
   <tr>
     <td>
       <input type="radio" name="color" id="val" value="red" >Red
     </td>
   </tr>
   <tr>
     <td>
       <input type="radio" name="color" value="blue" >Blue
     </td>
   </tr>
</table>
   <input type="submit" name="btn" value="update" />
</form>
...
于 2013-05-17T15:20:04.710 回答