0

我有一个问题:我创建了一个 UserControl A,A 有自己的控制器,以便在添加的每个视图中重用 Usercontrol 操作,UserControl A 需要一个模型 UserControlModel,我想要做的是更新包含视图的模型用户控件 A。

如何将值从 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="color-@Model.Id"><!-- 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")
4

1 回答 1

0

要回答有关 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);   
}
于 2012-05-21T08:04:11.143 回答