Asp.net mvc 从UserControl-mvc更新模型
我有一个问题:我创建了一个UserControl a,a有自己的控制器,以便在添加的每个视图中重用UserControl操作,UserControl a需要一个模型UserControlModel,我要做的是更新包含UserControl a的视图的模型 如何将值从UserControl传递到主视图或包含该用户控件的任何视图,以保持该值 一些代码: 用户控制控制器Asp.net mvc 从UserControl-mvc更新模型,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,我有一个问题:我创建了一个UserControl a,a有自己的控制器,以便在添加的每个视图中重用UserControl操作,UserControl a需要一个模型UserControlModel,我要做的是更新包含UserControl a的视图的模型 如何将值从UserControl传递到主视图或包含该用户控件的任何视图,以保持该值 一些代码: 用户控制控制器 public class ColorBlockUserControlController : Controller {
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);
}
}
公共类ColorBlockUserControlController:控制器
{
///
///ColorBlockUserControl的AJAX表单的回调方法。
///
///彩色模型
///要在目标DIV标记中显示的HTML字符串。
[HttpPost]
公共行动结果DrawColor(ColorModel模型)
{
if(ModelState.IsValid)
{
返回内容(ColorManager.GetGradientDiv(model.RGBColor、model.Width、model.Height));
}
其他的
{
返回重定向(“/”);
}
}
[接受动词(HttpVerbs.Post)]
公共JsonResult Sumar(整数高度、整数宽度)
{
返回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="color-@Model.Id"><!-- Will be populated by AJAX method --></div>
}
$(文档).ready(函数(){
var Valores={
身高:30,,
“宽度”:30
};
$(“#btnSumar”)。单击(函数(){
var targetDiv=“#TargetResult”;
美元邮政(
“/ColorBlockUserControl/Sumar”,
瓦洛雷斯,
功能(数据){
如果(数据===真){
调试器;
$(targetDiv.html('Sumado');
}
否则{
警报(“未能保存用户”);
}
},
“json”
);
});
});
@使用(Ajax.BeginForm(“DrawColor”、“ColorBlockUserControl”、新的AjaxOptions{UpdateTargetId=“color-”+Model.Id}))
{
RGB(示例:FFAA00)
@EditorFor(x=>x.RGBColor)
@Html.ValidationMessageFor(x=>x.RGBColor)
宽度
@EditorFor(x=>x.Width)
高度
@EditorFor(x=>x.Height)
这是一个测试
}
使用用户控件查看:
@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")
@model MVCColorUserControl.Models.HomeModel
@使用MVCColorUserControl.Models
@{
ViewBag.Title=“彩色演示”;
}
@Model.WelcomeText
部分控制
@Html.Partial(“UserControls/ColorBlockUserControl”,新的ColorModel())
@*
在服务器端初始化的部分控件
@{
RenderAction(“InitializeUserControl”);
}*@
@ActionLink(“Ir测试视图”、“测试”)
@ActionLink(“Usando otro控制器”、“InNewController”)
要回答有关FormCollection的问题,请执行以下操作:
在控制器中,您可以执行以下操作:
[HttpPost]
public ActionResult GetMeSomeValues(FormCollection formCollection)
{
string text = formCollection.Get("Name of the input or whatever you have in your form");
//same goes for int values which you will need to convert from a string.
//Now if you want to pass the value to a Model simply create a new Model in here
//and add it.
var model = new Model();
model.Value = text;
return View(model);
}
您可以使用
FormCollection
或使用强类型视图,另外,请在StackOverflow上发布相关代码。如果您不能真正理解您试图在此处实现的目标,请发布更详细的代码,指出您的问题(确切地说,没有垃圾代码)。当DrawColor操作运行时(在Usercontrol中)我想更新HomeModel值(在包含UserControl的视图中)