2014-03-07 81 views
0

中設置模型中的任何外部值我必須在模型中設置@ item.ID,就像我在EditorFor(model => model.Amount)中設置Amount一樣。我必須保存該數量並在數據庫中的ID,所以都應該在我的控制器的行動中發佈。在查看頁面

@foreach (var item in ViewBag.countries) 
{    
    <td><label class="control-label">@item.Name</label></td>            
    <td>@Html.EditorFor(model => model.Amount)</td> 
    <td><input type="text" /></td> 
    <td><button type="submit" class="btn-primary</i>Update</button></td>    
} 

我的模型是: -

public class AllocationViewModel 
    { 
     public long ID { get; set; } 
     public double Amount { get; set; } 
     public long CountryId { get; set; } 
    } 

在這裏,我不得不從viewbag的item.ID設置countryID模型。

+0

剛纔你的問題是什麼? – row1

+0

我必須從viewpage發佈@ item.ID作爲model.CountryID。 – user3206357

回答

1

在每個項目的隱藏字段中添加model.CountryId,以便您可以將其發佈到服務器。

@foreach (var item in ViewBag.countries) 
{    
    <td><label class="control-label">@item.Name</label></td>            
    <td>@Html.EditorFor(model => model.Amount)</td> 
    <td>@Html.HiddenFor(model => model.CountryId, new { @Value = item.ID })</td> 
    <td><input type="text" /></td> 
    <td><button type="submit" class="btn-primary</i>Update</button></td>    
} 
+0

我必須設置通過Viewbag.countries模型中的item.ID。 – user3206357

+0

@ user3206357,我更新了我的答案 –

+0

我在使用HiddenFor時遇到編譯錯誤 – user3206357