C# 获取/设置Razor视图中的整数值
我有一个元素列表,我想编辑它 我创建了这个片段:C# 获取/设置Razor视图中的整数值,c#,.net,asp.net-mvc,razor,C#,.net,Asp.net Mvc,Razor,我有一个元素列表,我想编辑它 我创建了这个片段: @{ ViewBag.Title = "Liste des comptes administrateurs"; Layout = "~/Views/Shared/_Layout.cshtml"; int i = 0; } <hgroup class="title"> <h1 style="color: darkcyan">@ViewBag.Title</h1> </hg
@{
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>
@{
ViewBag.Title=“公司管理人员名单”;
Layout=“~/Views/Shared/_Layout.cshtml”;
int i=0;
}
@视图包。标题
红色
蓝色
我的问题是,当我点击编辑按钮时,I
的值为0,即使我选中了其中一个单选按钮。其值未更改,始终为0
- 为什么
的值没有改变i
- 如何修复此错误
@{
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>
...
@{
如果(IsPost){
int i;
如果(请求表格[“颜色”]。等于(“红色”))
{
i=1;
}
其他的
{
i=2;
}
}
}
@视图包。标题
红色
蓝色
...
您可以使用FormCollection对象执行此操作
public ActionResult SomeActionMethod(FormCollection formCollection)
{
foreach (var key in formCollection.AllKeys)
{
var value = formCollection[key];
}
// or
var color=formCollection["color"];
}
您应该使用以下代码: 型号:
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");
}
}
}
您不能从客户端事件运行服务器端代码
@i=2
,单击onclick
@ShaiCohen好的,您有其他方法知道选中了哪个单选框吗?我如何将其发送到控制器,这是我的问题不要查看单选按钮的值,我需要知道哪一个是检查好的,但是对于
如何添加参数FormCollection FormCollection
我的意思是如何用FormCollection替换new{Id=@i}
。您只需访问formCollection[“Id”]
。