0
我有一個問題:我已經創建了一個UserControl A,A有它自己的控制器,以便在每個視圖中重新使用Usercontrol actions添加,UserControl A期望一個模型UserControlModel,我想要做的是更新包含用戶控件A的視圖的模型。更新來自UserControl的模型 - mvc
如何將UserControl的值傳遞到主視圖或包含用戶控件的任何視圖以保持該值?
一些代碼:
用戶控件控制器
public class ColorBlockUserControlController : Controller
{
/// <summary>
/// Callback method for ColorBlockUserControl's AJAX form.
/// </summary>
/// <param name="model">ColorModel</param>
/// <returns>HTML string to be dispalyed within target DIV tag.</returns>
[HttpPost]
public ActionResult DrawColor(ColorModel model)
{
if (ModelState.IsValid)
{
return Content(ColorManager.GetGradientDiv(model.RGBColor, model.Width, model.Height));
}
else
{
return Redirect("/");
}
}
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Sumar(int Height, int Width)
{
return Json(true);
}
}
用戶控件的HTML:
<script type="text/javascript">
$(document).ready(function() {
var Valores = {
'Height': 30,
'Width': 30
};
$("#btnSumar").click(function() {
var targetDiv = "#TargetResult";
$.post(
'/ColorBlockUserControl/Sumar',
Valores,
function (data) {
if (data === true) {
debugger;
$(targetDiv).html('Sumado');
}
else {
alert('Failed to save the user');
}
},
'json'
);
});
});
</script>
@using (Ajax.BeginForm("DrawColor", "ColorBlockUserControl", new AjaxOptions { UpdateTargetId = "color-" + Model.Id }))
{
<div>RGB (example: FFAA00)</div>
<div class="formLine">
@Html.EditorFor(x => x.RGBColor)
</div>
@Html.ValidationMessageFor(x => x.RGBColor)
<div>Width</div>
<div class="formLine">
@Html.EditorFor(x => x.Width)
</div>
<div>Height</div>
<div class="formLine">
@Html.EditorFor(x => x.Height)
</div>
<div id="btnSumar" style="cursor:pointer;">This is a Test</div>
<p><input type="submit" value="Color Me!" /></p>
<div id="[email protected]"><!-- Will be populated by AJAX method --></div>
}
查看與用戶控件:
@model MVCColorUserControl.Models.HomeModel
@using MVCColorUserControl.Models
@{
ViewBag.Title = "Color Demo";
}
<h2>@Model.WelcomeText</h2>
<p>
A Partial Control</p>
@Html.Partial("UserControls/ColorBlockUserControl", new ColorModel())
<hr />
@*<p>
A Partial Control that is initialized on Server-Side</p>
@{
Html.RenderAction("InitializeUserControl");
}*@
@Html.ActionLink("Ir a Test View", "Test")
@Html.ActionLink("Usando otro controller", "InNewController")
你可以使用'FormCollection'或使用強類型的意見,再加上請在這裏發表相關的代碼#2 – Rafay
我該怎麼辦呢? –
不能真正理解你在這裏試圖達到什麼目的,請張貼更詳細的代碼,指出你的問題(其確切地說是沒有垃圾代碼的地方)。 –