2014-04-03 46 views
0

您好,我有一个看法页:ASP.NET MVC 4更新DIV adtwer Aajax.Beginform

@using System.Collections.Specialized 

    @model USCSAR.ViewModel.ViewAddHardware 

    <div class="adminFormsContainer80"> 

    <center> 
     <h5> 
      @Resources.Localization.Equipment: @Model.Equipment.Manufacturer @Model.Equipment.Model 
     </h5> 
     <h6> 
      SN: @Model.Equipment.SerialNumber 
     </h6> 
     <h6> 
      @Resources.Localization.InventoryNumber: @Model.Equipment.InventoryNumber 
     </h6> 
    </center> 

    <hr /> 

    @if (Model != null) 
    { 
     using (Ajax.BeginForm("SaveHardware", "Equipment", new AjaxOptions 
                  { 
                   HttpMethod = "Post", 
                   UpdateTargetId = "grid", 
                   InsertionMode = InsertionMode.Replace, 
                   OnSuccess = "SetDefaultControlValue" 
                  })) 
     { 
      <table class="table table-striped"> 
       <tr> 
        <td> 
         @Html.Label("Hardware type: ") 
         @Html.DropDownListFor(model => model.HardwareID, Model.HardwareType) 
        </td> 
        <td> 
         @Html.Label("Name: ") 
         @Html.EditorFor(model => model.Name) 
        </td>     
        <td> 
         @Html.Label("SN: ") 
         @Html.EditorFor(model => model.SerialNumber) 
        </td> 
        <td> 
         @Html.Label("Inventory number: ") 
         @Html.EditorFor(model => model.InventoryNumber) 
        </td> 
        <td> 
         @Html.Label("Installed: ") 
         @Html.EditorFor(model => model.IsInstaled) 
        </td> 
       </tr> 
      </table> 

      <div id="error"> 
       <center> 
        <p id="errorMassage" class="loginErrorMessage"> 
         @Model.Error 
        </p> 
       </center> 
      </div> 

      <center> 
       <input class="btn btn-primary" type="submit" value="@Resources.Localization.AddNew" /> 
      </center> 
     } 
    } 

</div> 

<div id="grid" class="adminFormsContainer80"> 
    @{ Html.RenderAction("_AddedHardwareGrid"); } 
</div> 

@section scripts 
{ 
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> 

    <script src="~/CustomScripts/SetDefaultControlValue.js"></script> 


    <script> 

     $(function() 
     { 
      SetDefaultControlValue(); 
     }); 

    </script> 
} 

的控制器:

 [HttpGet] 
     [Authorize(Roles = "Admin, SuperAdmin")] 
     public ActionResult AddHardware(int id) 
     { 
      try 
      { 
       Common.SessionHelper.EquimpentID = id; 
      } 
      catch (Exception) 
      { 
       return View("Error"); 
      } 

      return View(new ViewModel.ViewAddHardware()); 
     } 

     [HttpPost] 
     [Authorize(Roles = "Admin, SuperAdmin")] 
     public ActionResult SaveHardware(ViewModel.ViewAddHardware item) 
     { 
      try 
      { 
       if (item != null) 
       { 
        int equipmentID = item.Equipment.EquipmentID; 

        int hardwareID = item.HardwareID; 

        ViewModel.ViewAddHardware hardware = manager.GetHardwareForEquipmentByEIDandHID(equipmentID, hardwareID); 

        if (hardware == null) 
        { 
         manager.SaveHardwareForEquipment(item); 
        } 
        else if (hardware != null && string.IsNullOrEmpty(item.Error)) 
        { 
         item.Error = Resources.Localization.HardwareExist; 
         return View("AddHardware", item); 
        } 
        else if (hardware != null && !string.IsNullOrEmpty(item.Error)) 
        { 
         manager.SaveHardwareForEquipment(item); 
        } 

        List<ViewModel.ViewAddHardwareGrid> model = manager.GetHardwareForEquipmentByEquipmentID(equipmentID); 

        return PartialView("_AddedHardwareGrid", model); 
       } 
      } 
      catch (Exception) 
      { 
       return View("Error"); 
      } 

      return View("AddHardware", item); 
     } 

我woluld喜欢才达到,该代码执行时在控制器

else if (hardware != null && string.IsNullOrEmpty(item.Error)) 
        { 
         item.Error = Resources.Localization.HardwareExist; 
         return View("AddHardware", item); 
        } 

在视图下面更新DIV的模型错误消息:

<div id="error"> 
       <center> 
        <p id="errorMassage" class="loginErrorMessage"> 
         @Model.Error 
        </p> 
       </center> 
      </div> 

日Thnx

+0

你有什么问题?请编辑您的标题 – Bellash

+0

对不起,问题是如何在ajax post响应后更新“错误,, div的内容。 – Wasyster

回答

0

很明显,你必须使用客户端的JavaScript代码,但不使用ASP.Net MVC结构来做到这一点。假设您呼叫使用jQuery您的控制器,您可以尝试下面的代码:

$("#errorMessage").html(errorMessage);

其中errorMessage是从服务器收到一个包含字符串错误消息。

+0

正如我所看到的,Error属性取回,它在模型中,但它不显示,并且$(“#errorMessage”).html(errorMessage);我不需要为我工作。我需要显示信息,我需要这个信息再次回到控制器,但它不是。 – Wasyster