简体   繁体   中英

Getting/setting the value of an integer inside a Razor view

I have aa list of elements and I would like to edit it.

I created this snippet:

@{
    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>

My problem is that when I click into the button of edition the value of i is 0 even I checked one of the radio buttons. Its value didn't change and always is 0.

  • Why the value of i didn't change?
  • How can I fix this error?

You can do it with a FormCollection object.

public ActionResult SomeActionMethod(FormCollection formCollection)
{
  foreach (var key in formCollection.AllKeys)
  {
    var value = formCollection[key];
  }
// or 
var color=formCollection["color"];
}

You should use some codes like these:

Model:

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;

    }


}
}

View:

@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>

Controller:

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");
    }

}
}    

Use this code:

@{
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>
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM