Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 17th, 2012  |  syntax: None  |  size: 3.59 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Update Navigation Properties with Entity Framework (MVC3 Razor)
  2. @model  PDR.Models.RateProfile
  3.  
  4. @using (Html.BeginForm()) {
  5. @Html.ValidationSummary(true)
  6. <fieldset>
  7.     <legend>RateProfile</legend>
  8.  
  9.     @Html.HiddenFor(model => model.RateProfileID)
  10.     @Html.HiddenFor(model => model.LoginID)
  11.  
  12.     <div class="editor-label">
  13.         @Html.LabelFor(model => model.ProfileName)
  14.     </div>
  15.     <div class="editor-field">
  16.         @Html.EditorFor(model => model.ProfileName)
  17.         @Html.ValidationMessageFor(model => model.ProfileName)
  18.     </div>
  19.  
  20.     <div class="editor-label">
  21.         @Html.LabelFor(model => model.isDefault)
  22.     </div>
  23.     <div class="editor-field">
  24.         @Html.EditorFor(model => model.isDefault)
  25.         @Html.ValidationMessageFor(model => model.isDefault)
  26.     </div>
  27.     <div>
  28.         <fieldset>
  29.         <legend>Dime</legend>
  30.         <table>
  31.             <tr>
  32.                 <th>
  33.                     Min
  34.                 </th>
  35.                 <th>
  36.                     Max
  37.                 </th>
  38.                 <th>
  39.                     Price
  40.                 </th>
  41.                 <th></th>
  42.             </tr>
  43.         @foreach (var rate in Model.Rates)
  44.         {
  45.             <tr>
  46.                 <td>
  47.                     @Html.EditorFor(modelItem => rate.minCount)
  48.  
  49.                     @Html.ValidationMessageFor(model => rate.minCount)
  50.                 </td>
  51.                 <td>
  52.                     @Html.EditorFor(modelItem => rate.maxCount)
  53.                     @Html.ValidationMessageFor(model => rate.maxCount)
  54.                 </td>
  55.                 <td>
  56.                     @Html.EditorFor(modelItem => rate.Amount)
  57.                     @Html.ValidationMessageFor(model => rate.Amount)
  58.                 </td>
  59.             </tr>
  60.         }
  61.         </table>
  62.     </fieldset>
  63.  
  64.     <p>
  65.         <input type="submit" value="Save" />
  66.     </p>
  67. </fieldset>
  68. }
  69.        
  70. [HttpPost]
  71.     public ActionResult Edit(RateProfile rateprofile)
  72.     {
  73.  
  74.         if (ModelState.IsValid)
  75.  
  76.         {
  77.             db.Entry(rateprofile).State = EntityState.Modified;
  78.  
  79.             foreach (Rate rate in rateprofile.Rates)
  80.             {
  81.                 db.Entry(rate).State = EntityState.Modified;
  82.             }
  83.             db.SaveChanges();
  84.             return RedirectToAction("Index");
  85.         }
  86.         return View(rateprofile);
  87.     }
  88.        
  89. public partial class Rate
  90. {
  91.     public int RateID { get; set; }
  92.     public int RateProfileID { get; set; }
  93.     public string Size { get; set; }
  94.     public decimal Amount { get; set; }
  95.     public int minCount { get; set; }
  96.     public int maxCount { get; set; }
  97.     public int PanelID { get; set; }
  98.  
  99.     public virtual Panel Panel { get; set; }
  100.     public virtual RateProfile RateProfile { get; set; }
  101. }
  102.  
  103. public partial class RateProfile
  104. {
  105.     public RateProfile()
  106.     {
  107.         this.Rates = new HashSet<Rate>();
  108.     }
  109.  
  110.     public int RateProfileID { get; set; }
  111.     public string ProfileName { get; set; }
  112.     public int LoginID { get; set; }
  113.     public bool isDefault { get; set; }
  114.  
  115.     public virtual Login Login { get; set; }
  116.     public virtual ICollection<Rate> Rates { get; set; }
  117. }
  118.        
  119. @for (var int i = 0; i < Model.Rates; i++)
  120. {
  121.     <tr>
  122.         <td>
  123.             @Html.EditorFor(modelItem => Model.Rates[i].minCount)
  124.             @Html.ValidationMessageFor(model => Model.Rates[i].minCount)
  125.         </td>
  126.         <td>
  127.             @Html.EditorFor(modelItem => Model.Rates[i].maxCount)
  128.             @Html.ValidationMessageFor(model => Model.Rates[i].maxCount)
  129.         </td>
  130.         <td>
  131.             @Html.EditorFor(modelItem => Model.Rates[i].Amount)
  132.             @Html.ValidationMessageFor(model => Model.Rates[i].Amount)
  133.         </td>
  134.     </tr>
  135. }