在视图和控制器之间传递模型 [英] Pass model between view and controller

查看:88
本文介绍了在视图和控制器之间传递模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MVC中修改对象属性值。

I want to modify an object property values in MVC.

@model Test.Web.Models.CountryLanguageModel

<div class="col-md-8 col-md-offset-2">
        @using (Html.BeginForm())
        {
            @Html.ValidationSummary(true)
            <fieldset>
                <legend>Language Edit</legend>

                @Html.HiddenFor(model => model.CountryId)

                <div class="editor-label">
                    @Html.LabelFor(model => model.CountryLanguage.LanguageName)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.CountryLanguage.LanguageName)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.CountryLanguage.Message)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.CountryLanguage.Message)
                </div>
          
                <br />
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Save" class="btn btn-default" />
                    </div>
                </div>
            </fieldset>
        }
    </div>





在控制器中。我有两种方法。



And in controller. I have two methods.

public ActionResult LanguageEdit(int id)
       {
           var model = new CountryLanguageModel();
           model.CountryLanguageId = id;
           var CountryLanguage = CountryService.Get(x => x.LanguageId == id);
           model.CountryLanguage = CountryLanguage;
           return View(model);
       }

       [HttpPost]
       public ActionResult LanguageEdit(CountryLanguageModel CountryLanguageModel)
       {
           if (ModelState.IsValid)
           {
           }
           var model = new CountryLanguageModel();
           model.CountryLanguageId = CountryLanguageModel.CountryLanguageId;
           var CountryLanguage = CountryService.Get(x => x.LanguageId == model.CountryLanguageId);
           model.CountryLanguage = CountryLanguage;
           return View(model);
       }





在第一种方法中,它正常工作



In the first method, it works correctly

public ActionResult LanguageEdit(int id)



我的问题在于第二种方法


My question is in the second method

public ActionResult LanguageEdit(CountryLanguageModel CountryLanguageModel)



我没有看到模特的变化。



我尝试过:



例如,我在第二种方法中找到了。 model.CountryLanguage 为空。这意味着我无法通过该模型。


I don't see the changes in the model.

What I have tried:

For example, I found in the second method. model.CountryLanguage is null. That means I failed to pass the model.

推荐答案

您的问题是MVC中的模型绑定如何处理复杂对象以便在HTML助手中使用,这就是您所拥有的继续在你看来。由于你没有发布你的模型,我猜你的模型看起来像



Your issue is with the how model binding in MVC handles complex objects for use in the HTML helpers which is what you have going on in your view. Since you didn't post your model, I am guessing your model looks something like

public class CountryLanguageModel
{
   public CountryLanguage CountryLanguage {get;set;}
}

public class CountryLanguage
{
   public string LanguageName {get;set;
}





因为你的视图实现了 @ Html.EditorFor(model => model.CountryLanguage .LanguageName)它将在HTML中查找类似< input type =textid =CountryLanguage_LanguageNamename =CountryLanguage_LanguageName/>



当您在Post动作中查看CountryLanguage模型时,该类可能没有的属性名称公共字符串CountryLanguage_LanguageName 相反,你的Post动作正在寻找通过幕后模型绑定发布到该动作的属性,只需 LanguageName





这里的简单解决方法是将您的视图更改为使用您的模型 @Model CountryLanguage 或者如果不可能,创建一个局部视图,将 Model.CountryLanguage 从父视图传递到您创建的局部视图,以便安抚模型绑定MVC。这样做是将CountryLanguage_LanguageName的上述示例更改为类似 @ Html.EditorFor(model => model.LanguageName),这使得html看起来像< input type =textid =LanguageNamename =LanguageName/>



其他选项是不使用强类型视图,而是使用 @ Html.Editor 而不是 @Html.EditorFor 这看起来像 @Html.Editor(LanguageName,@ Model.CountryLanguage.LanguageName)哪个应该(我要记忆)渲染HTML看起来像< input type =textname =LanguageNameid =LanguageName/>



如果您无法使用这些选项,那么您需要在MVC中查看自定义模型绑定/属性映射选项。就个人而言,如果遇到此问题,我会选择部分视图的路径。



这些是如果您选择使用自定义模型绑定路线,快速搜索中的一些链接可能会有所帮助:



asp.net mvc 4 - 下划线字符串模型绑定器 - 堆栈溢出 [ ^ ]



[ ^ ]



编辑:



再次查看你的帖子,看到它是CountryLanguageModel,所以无论出于何种原因你的帖子action需要CountryLanguageModel的复杂类对象和CountryLanguage类作为LanguageModel的属性,那么你可能需要查看自定义模型绑定解决方案,但如果可能的话,我还会看一下并重新考虑您的类结构。



Because your view implements @Html.EditorFor(model => model.CountryLanguage.LanguageName) its going to look in HTML as something like <input type="text" id="CountryLanguage_LanguageName" name="CountryLanguage_LanguageName" />.

When you look at your CountryLanguage model in your Post action, chances are that class doesn't have a property name of public string CountryLanguage_LanguageName rather, your Post action is looking for properties posted to that action via the behind the scenes model binding to be simply LanguageName.


The easy fix here would be to change your view to either use your model to be @Model CountryLanguage or if that isn't possible, create a partial view, pass in Model.CountryLanguage from the parent view into the partial view you create in order to appease the model binding of MVC. What this would do is change the above example of CountryLanguage_LanguageName to look something like @Html.EditorFor(model=>model.LanguageName) which renders html to look something like <input type="text" id="LanguageName" name="LanguageName" />

The other option is to not use a strongly typed view and instead you specify the attributes by using @Html.Editor instead of @Html.EditorFor This would look something like @Html.Editor("LanguageName", @Model.CountryLanguage.LanguageName") which should (i'm going off of memory) render HTML that looks something like <input type="text" name="LanguageName" id="LanguageName"/>

If those options are not possible for you then you need to look into custom model binding/property mapping options within MVC. Personally, if i ever run into this issue i go the route of partial views.

These are a few links from a quick search that may be of help if you choose to go the custom model binding route:

asp.net mvc 4 - Underscore string model binder - Stack Overflow[^]

[^]



Looked at your post action again and saw it is CountryLanguageModel, so if for whatever reason your Post action does require the complex class object of CountryLanguageModel with CountryLanguage class as a property of the LanguageModel, then you may have to look into a custom model binding solution, but I would also take a look and re-think your class structure if possible.


这篇关于在视图和控制器之间传递模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆