【问题标题】:Change a specific column in table in ASP.NET MVC在 ASP.NET MVC 中更改表中的特定列
【发布时间】:2017-01-10 21:55:25
【问题描述】:

我是 ASP.NET MVC 的新手。试图从表中的记录列表中获取记录。在每条记录中,我都有一个字段status,该字段设置为1。这里我只显示ViewButton 中的一条记录。当我单击一个按钮时,它必须将该记录的状态更改为数据库中的 3,然后在同一视图中获取另一条记录。目前我通过选择脚手架模板列表创建了这个视图。

控制器:

public class HomeController : Controller
{
    dbSampleEntities db = new dbSampleEntities ();
    //
    // GET: /Home/
    public ActionResult Index()
    {
        var result = db.Visits.Where(x=> x.Status == "1").OrderBy(r => Guid.NewGuid()).Take(1);
        return View(result);
    }

    [HttpPost]
    public ActionResult ChangeStatus(int VisitorID)
    {           
         return RedirectToAction("Index");         
    }
}

Index.html:

 @model IEnumerable<SampleApp.Visit>

@{
    ViewBag.Title = "Index";
}

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.VisitorID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Phone)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.VisitTypeID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.BranchID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Status)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.EmailID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Comments)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.UserCreated)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CreatedTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.UpdatedTime)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.VisitorID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Phone)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.VisitTypeID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.BranchID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Status)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EmailID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Comments)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UserCreated)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CreatedTime)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UpdatedTime)
            </td>
            <td>
                @using (Html.BeginForm("ChangeStatus", "Home", FormMethod.Post))
                {
                    @Html.HiddenFor(modelItem => item.VisitorID)
                    <input type="submit" value="Change Status" />
                }
            </td>
        </tr>
    }

</table>

这里ChangeStatus 正在调用但得到空值。帮助我在哪里弄错了。

【问题讨论】:

  • 尝试在ChangeStatus 中将return View(); 更改为return RedirectToAction('Index')
  • 感谢您的回复。我知道。但是在那个 POST 方法中,我必须将状态列从 1 更新到 3。但是我得到 ChangeStatus(Visit visit) 中的所有值都是空的

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


【解决方案1】:

表单正在发布到ChangeStatus,但没有表单值被发布。没有要发布的数据,因为表单没有实际的表单元素。所以框架没有可以用来构造Visit实例的值。

由于您只是对记录执行单个预定义操作,因此您真正需要的是该记录的标识符。您不需要完整的记录。使用该标识符,您将从数据库中获取记录,对其进行更新并保存。

因此删除整个表单并为每个表格行添加一个,其中您将包含一个用于标识符的表单元素:

<td>
    @using (Html.BeginForm("ChangeStatus", "Home", FormMethod.Post))
    {
        @Html.HiddenFor(modelItem => item.VisitorID)
        <input type="submit" value="Change Status" />
    }
</td>

(注意:我猜测 VisitorID 是记录的标识符。虽然命名并没有完全符合 100%。使用任何存在的标识符或标识符集记录。)

然后在动作中只需要标识符:

[HttpPost]
public ActionResult ChangeStatus(int visitorID)
{
    // use the visitorID to fetch the record that you want to update
    return RedirectToAction('Index')
}

【讨论】:

  • 按你说的试过了。但出现此错误参数字典包含“NiranjanApp.Controllers”中方法“System.Web.Mvc.ActionResult ChangeStatus(Int32)”的不可空类型“System.Int32”的参数“id”的空条目。家庭控制器”。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数
  • @NNR:展示你的尝试。参数的名称和表单元素的名称必须匹配,这就是框架知道如何使用它们的方式。
  • 通过更改更新了我的问题
  • @NNR:很有趣。并且错误正在寻找参数id,但没有该名称的参数? form 元素中生成的 POST 请求的 URL 是什么?也许尝试将方法参数从VisitorID 重命名为id?主要是猜测。
猜你喜欢
  • 2016-11-05
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多