如何使用ASP.NET MVC的ViewModels?

问题描述:

我刚开始学习ASP.NET MVC中的ViewModels。所以,我想实现如下样本的例子:

I just started learning about ViewModels in ASP.NET MVC. So, I thought of implementing a sample example as below:

业务实体

public class AddModel
{
    public int a { get; set; }
    public int b { get; set; }

    public int Add()
    {
        return (this.a + this.b);
    }
}

添加视图模型

public class AddViewModel
{
    public AddModel addModel;
    public int Total { get; set; }
}

控制器

public class AddController : Controller
{
    [HttpPost]
    public JsonResult Add(AddViewModel model)
    {

        int iSum = model.addModel.a + model.addModel.b;
        model.Total = iSum;
        return Json(model);

    }

    public ActionResult Index()
    {
        return View();
    }
}

查看实施

@model ViewModelApplication.AddViewModel
<script type="text/javascript" src="../../Scripts/MicrosoftAjax.js"></script>
<script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<script type="text/javascript">
    function Callback(data) {
        alert("I am sucess call");
    }

    function Failed() {
        alert("I am a failure call");
    }
</script>

@using (Ajax.BeginForm("Add", "Add", new AjaxOptions { OnSuccess = "Callback", OnFailure = "Failed" }))
{
    <table align="center">
        <tr>
            <td class="tdCol1Align">
                <label>
                    Number1</label>
            </td>
            <td class="tdCol2Align">
                @Html.TextBoxFor(Model => Model.addModel.a)
            </td>
        </tr>
        <tr>
            <td class="tdCol1Align">
                <label>
                    Number2</label>
            </td>
            <td class="tdCol2Align">
                @Html.TextBoxFor(Model => Model.addModel.b)
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="submit" value="Add" class="button" />
            </td>
        </tr>
    </table>
}

这里的问题是,我无法检索进入每当点击添加按钮文本框中的值;相应的AJAX行动正在它。

The problem here is that I am unable to retrieve the values entered into the text boxes whenever the Add button is clicked; the corresponding AJAX action is being it.

当我尝试访问的值 A B ,我得到空值,而不是值进入文本框。

When I try to access the values of a and b, I get nulls instead of the values entered into the text boxes.

我不知道我要去的地方错了。请帮助。

I am not sure where I am going wrong. Please help.

您的视图模型应该是这样的。

your view model should look like this

public class AddViewModel
    {
        public int a { get; set; }
        public int b { get; set; }
        public int Total { get; set; }
    }

和在CSHTML

            <td class="tdCol2Align">
                @Html.TextBoxFor(m=> m.a)
            </td>

            <td class="tdCol2Align">
                @Html.TextBoxFor(m=> m.b)
            </td>

在控制器

        [HttpPost]
        public JsonResult Add(AddViewModel model)
        {
            int iSum = model.a + model.b;
            model.Total = iSum;
            return Json(model);

        }

修改

查看模式是有渲染你的观点并不里面放置任何逻辑。如果你有更复杂的模型那么这将是很难地图模式视图模型。为此,您可以使用AutoMapper或ValueInjector的模型和视图模型之间的映射。

View model is there to render your views don't place any logic inside that. if you have more complex model then it will be hard to map Model with ViewModel. for this you can use AutoMapper or ValueInjector for mapping between model and view model.

链接automapper HTTP://automapper.$c$cplex.com/

link for automapper http://automapper.codeplex.com/

链接价值注入 HTTP://valueinjecter.$c$cplex.com/

希望这有助于