在razor中从视图中获取整数的false值

时间:2013-07-22 10:02:36

标签: c# asp.net-mvc asp.net-mvc-3

我的asp.net mvc应用程序出错了。我有这个观点:

@if (Model == null || Model[1] == null || Model[1].Count == 0) 
{
    <label>Pas de demande</label>
                         }
else{
    int i = 1;
    string s = "";
    <form action="\Travail\Validation_Reservation" method="post">
@foreach( Planning.Models.Affectation v in Model[1]){
    if (v.Periode == 1) { s = "matinale"; }
    else { s = " Après midi"; }
    int a = v.Id_affectation; 
     <div><h5 style="color:blue">@i / Demande de reservation de vehicule @veh.Matricule : le @v.Date_affectation , la periode @s</h5>
    <input type="hidden" name="id" value="@a" />
    <input type="submit" name="btn" value="valider" />
   <input type="submit" name="btn" value="refuser" /></div>
    <br />
    i++;
     }
        </form>

和这个动作:

public ActionResult Validation_Reservation() {
            string btn = Request.Params["btn"];
            string id_string = Request.Params["id"];
            int id_affectation = int.Parse(id_string);
            Owner resp = new Owner();
            if (btn == "valider") {
                resp.Accepter_Reservation(id_affectation);
                return RedirectToAction("Notification");
                  }
            resp.Refuser_Reservation(id_affectation);
            return RedirectToAction("Notification");
         }

我的问题在string id_string = Request.Params["id"];id_string取值为1,2,但整数id_affectation的值为1和{{1} }。

为什么会这样?我该如何解决?

1 个答案:

答案 0 :(得分:0)

我找到了这个解决方案, 我必须更改formloop的顺序才能获得唯一的id

@foreach( Planning.Models.Affectation v in Model[1]){
<form action="\Travail\Validation_Reservation" method="post">

    if (v.Periode == 1) { s = "matinale"; }
    else { s = " Après midi"; }
    int a = v.Id_affectation; 
     <div><h5 style="color:blue">@i / Demande de reservation de vehicule @veh.Matricule : le @v.Date_affectation , la periode @s</h5>
    <input type="hidden" name="id" value="@a" />
    <input type="submit" name="btn" value="valider" />
   <input type="submit" name="btn" value="refuser" /></div>
    <br />
    i++;

        </form>
}