mercredi 9 avril 2014

c# - ASP.NET MVC 4 mise à jour div adtwer Aajax.Beginform - Stack Overflow


Hi, I have a View page:


    @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>
}

The Controllers:


        [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);
}

I woluld like to achive, that when this code executes in Controller


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

in the View the DIV below update the Models error message:


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

thnx




It is obvious that you have to do it using client-side JavaScript code, but not using ASP.Net MVC constructions. Assuming that you are calling your controller with jQuery, you may try the following code:


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


where errorMessage is a string containing error message received from the server.



Hi, I have a View page:


    @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>
}

The Controllers:


        [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);
}

I woluld like to achive, that when this code executes in Controller


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

in the View the DIV below update the Models error message:


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

thnx



It is obvious that you have to do it using client-side JavaScript code, but not using ASP.Net MVC constructions. Assuming that you are calling your controller with jQuery, you may try the following code:


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


where errorMessage is a string containing error message received from the server.


0 commentaires:

Enregistrer un commentaire