更改ASP.NET MVC中表中的特定列

时间:2016-09-02 17:52:49

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

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

控制器:

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正在调用但是获取空值。帮助我在哪里弄错了。

1 个答案:

答案 0 :(得分: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')
}