由于字符串ID,CRUD Edit无法检索数据

时间:2018-07-19 00:49:41

标签: c# asp.net-mvc

我有一个名为SectionUser的表,

SectionUser

  • USER_ID(varchar(255))(主键)
  • NAME(varchar(255))

以下是示例值:

<script src="./node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"></script>

我在控制器中有一个create动作,可以成功创建部分用户。我的主要问题是编辑时。我无法获得价值。因此,例如,我输入了错误的USER_ID,并且想要对其进行编辑。 USER_ID始终为null。这意味着我要编辑主键。

控制器:

USER ID | NAME

EVTAB | ELMER TABER

FAYKVIL |  FAYK VILLET

查看(cshtml)

public ActionResult EditUser(string USER_ID)
{
    if (USER_ID == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    SectionUser section = db.SectionUsers.Find(USER_ID);
    if (section == null)
    {
        return HttpNotFound();
    }
    return View(section);
}

查看(索引)

@using (Html.BeginForm("EditUser", "Admin", FormMethod.Post,
                                  new { enctype = "multipart/form-data" }))
 {
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Section</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.USER_ID)

    <div class="form-group">
        @Html.LabelFor(model => model.USER_ID, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.USER_ID, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.USER_ID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.NAME, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.NAME, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.NAME, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}

1 个答案:

答案 0 :(得分:1)

所以我的问题是我在index.cshtml上使用的参数错误。这是旧的代码:

@Html.ActionLink("Edit", "EditUser", new { id = item.USER_ID.ToString().Trim() })

新代码:

@Html.ActionLink("Edit", "EditUser", new { USER_ID = item.USER_ID.ToString().Trim() })

正如您在EditUser Action中看到的那样,该参数是字符串USER_ID。