English 中文(简体)
MVC: 回返 供控制员使用的数据
原标题:MVC: Returning Multiple Rows of data to controller

我的表格有几行数据,我需要回到控制器。 我认为,我最初通过选定一个期限和点击一个吨来填表。 表格载有我的所有相关记录,但我的桌子之一载有一份下级清单。 因此,我应该能够选择点击“更新日期”,我的控制者可以避免变化。

因此,在我尝试和挽救之前,一切都发挥了作用。 发给控制员的模式完全无效。 我与桌子捆绑在一起的财产归还控制人。

 @ModelType SuperViewModel
 //We need this a view model in order to store a List of the models in the table   

 @Using (Html.BeginForm())
 @For Each i in Model.CompleteList
    Dim currentItem = i //MVC auto-generated extra declarations. Seems redundant to me but it works.
 @<table>
 <tr>
 <td>@Html.DisplayFor(function(Model)currentItem.Name)</td>
 <td>@Html.DisplayFor(function(Model)currentItem.SampleTime)</td>
 <td>@Html.DropDownListFor(function(Model)currentItem.WorkTime, ViewBag.WorkTimeList)</td>
 </tr>
 Next
 </table>
 <input name="submit" type="submit" value="Update"/>
 End Using

 //Controller
 <HttpPost()>
 function Save(vmodel as SuperViewModel, submit as String) as ActionResult //NOTE: submit parameter is used because we have two submit buttons but its not relevant here
      if submit = "Update"
            db.Entry(vmodel.CompleteList).State = EntityState.Modified//Here the exception is throw because our list is null at this point even tho its tied to the model in the view.
            db.SaveChanges()
      end if
 End Function

NOTE: This is written in VB. 该网络,但C#帮助值得欢迎。 我熟悉MVC两种语言。

最佳回答

您需要使用一台打印机,并且认为该指数元件用于超文本部分。 我不知道VB。 NET syntax, c# example is below. 这使模型约束器能够正确确定观点模式中的内容,从而可以重新推广关于背后的观点模式。

<table>
 @For(var i = 0; i < Model.CompleteList.Count; i++) 
{
 <tr>
 <td>@Html.DisplayFor(Model.CompleteList[i].Name)</td>
 <td>@Html.DisplayFor(Model.CompleteList[i]..SampleTime)</td>
 <td>@Html.DropDownListFor(Model.CompleteList[i]..WorkTime, ViewBag.WorkTimeList)</td>
 </tr>
}
 </table>

之后,你的职位方法应当进行罚款。

问题回答

由于在参数名称上的一些冲突,它可能无法返回(所使用的参数名称也必须在其他地方使用)。 引言

<input name="subButton" type="submit" value="Update"/>

变化

function Save(vm as SuperViewModel, subButton as String) as ActionResult

我需要看到<代码>产生的标识中的名称。 DropDownListFor,但我怀疑这是因为有 CurrentItem。 页: 1 SuperViewModel category. 你们在守则中没有任何地方表明,我看到你对<条码>具有约束力。 SuperViewModel category.

3. 将你的控制器方法改为:

<HttpPost()>
 function Save(currentItem as CompleteList, submit as String) as ActionResult //NOTE: submit parameter is used because we have two submit buttons but its not relevant here
      if submit = "Update"
            db.Entry(vmodel).State = EntityState.Modified//Here the exception is throw because our list is null at this point even tho its tied to the model in the view.
            db.SaveChanges()
      end if
 End Function

The Worktime property should be populationed with the selected Value from the DropDownList.

<Edit:更改了参数名称,使之与具有约束力的名称相符。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签