Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 我是使用一个表单还是每个表行使用一个asp.net mvc_Html_Asp.net Mvc_Standards - Fatal编程技术网

Html 我是使用一个表单还是每个表行使用一个asp.net mvc

Html 我是使用一个表单还是每个表行使用一个asp.net mvc,html,asp.net-mvc,standards,Html,Asp.net Mvc,Standards,我刚刚发现自己在这样做: <table> <tr> <th>Code</th> <th>Enabled</th> <th>Percentage</th> <th>Amount</th> <th>Start</th> <th>End</th>

我刚刚发现自己在这样做:

<table>
   <tr>
      <th>Code</th>
      <th>Enabled</th>
      <th>Percentage</th>
      <th>Amount</th>
      <th>Start</th>
      <th>End</th>
      <th>Action</th>
   </tr>
   @foreach(var item in Model)
   {
      <tr>
         <td>@Html.ActionLink(item.Code, "Edit", new { id = item.Id })</td>
         <td>@item.Enabled ? "Yes" : "No"</td>
         <td>@item.Percentage</td>
         <td>@item.Amount</td>
         <td>@item.StartDate</td>
         <td>@item.EndDate</td>
         <td>
            @using (Html.BeginForm("Delete", "Discounts", new { id = item.Id }))
            {
               <input type="submit" value="Delete" />
            }
         </td>
      </tr>
   }
</table>

代码
启用
百分比
数量
开始
终点
行动
@foreach(模型中的var项目)
{
@ActionLink(item.Code,“编辑”,新的{id=item.id})
@项目。启用?“是”:“否”
@项目.百分比
@项目.金额
@项目开始日期
@item.EndDate
@使用(Html.BeginForm(“删除”,“折扣”,新的{id=item.id}))
{
}
}
我停下来的原因是因为我每行都有一个表单,作为一个好的实践,将整个表包装成一个表单会更好吗


我可能已经知道答案了,但我想检查一下:-)

我想这取决于你的具体情况

  • 如果需要最小化页面大小,可以选择一种表单。但是,在提交表单之前,您需要使用javascript更新一些(隐藏的)字段
  • 另一方面,即使用户关闭了javascript,您当前的方法仍然有效

根据我目前对你的情况的了解,我可能会选择你现在拥有的东西,因为它很好,很干净

我认为你的方法很好,通常是最好的方法。e、 g.如果您正在编写一个使用stackoverflow这样的投票系统的应用程序(选择的示例是因为您现在正在查看这样的应用程序),并且您希望通过使用
HttpPost
实现投票机制,那么您可以创建一个小控件,将向上和向下按钮作为单独的表单*。通过这种方式,您可以轻松地将任意数量的此类“小部件”添加到页面中,而包含这些小部件的页面不需要知道任何关于它们的信息(包括它们是否存在)。事实上,如果关闭javascript,您可以使用不引人注目的javascript提交表单并重新加载投票“小部件”,并通过回切重新加载页面,这一切看起来都很不错

*注意:我不认为stackoverflow是这样做的,请注意

非AJAX故障回复可能如下所示:

[HttpPost]
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public ActionResult DoThing(whatever params you are passing in)
{
  // Do stuff and then...
  if (Request.IsAjaxRequest)
  {
      return View("_partialThingView", thingViewModel);
  }
  else
  {
      RedirectToAction("Index");
  }
}

我认为这是最好的方法,除非你想实现一个批量删除功能(你的HTML代码告诉我情况并非如此)

通过对每个项目使用表单(带有POST方法),您可以确保

  • 一次只删除一项
  • 您不会通过调用http://[yoursite]/[yourapp]/[delete\u method]/[item\u id\u to\u delete]等url意外删除项目

所以,继续你目前的方法。(我的0.02欧元版)

如果每一行的工作不同,那么没有理由不在每一行中使用不同的表单。+1用于提及Ajax请求和有用的代码片段:-)