返回部分视图和模型的动作 [英] Action returning Partial view and model

查看:90
本文介绍了返回部分视图和模型的动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC 3的新手,我对正确的方法有疑问。

I'm new to MVC 3 and I have a question regarding the correct approach.

想象一下我有一个模型:

Imagine I have a model:

public class MyCustomModel
{
       [Required]
        public string UserName { get; set; }

        [Required]
        public DateTime? Birthdate { get; set; }

        [Required]
        public string City {get;set;} //To partial view

        [Required]
        public string Street {get;set;} //To partial view  
  }

在这里,我有一个视图



@ Html.TextBoxFor(m => m.UserName)




@ Html.TextBoxFor(m => m.BirthDate)


@ Html.Action( LocationGroup, Home)//在这里应该是城市和街道被渲染

@Html.TextBoxFor(m => m.UserName) @Html.TextBoxFor(m => m.BirthDate) @Html.Action("LocationGroup", "Home") //In this should the city and street be rendered

我的局部视图会像这样:


@ Html.TextBoxFor(m => m.City)




@ Html.TextBoxFor(m => m.Street)

My Partial View will have somethign like that: @Html.TextBoxFor(m => m.City) @Html.TextBoxFor(m => m.Street)

这是控制器中的操作:

    [ChildActionOnly]
    public ActionResult LocationGroup()
    {
        MyCustomModel model = new MyCustomModel (); //Should i really instantiate a new instace of the model??? and pass it to the partial view
        return PartialView("_TempView", model);
    }

基本上,我的一般视图将包含texboxex的所有字段,但现在在我的部分视图我也希望能够正确呈现模型中的这些属性,并且在提交表单之后,该属性应该与所有其他属性在同一模型中可用。

Basically my general view will have all the field with texboxex, but now in my partial view i also would like to have few of those propeties from my model be rendered correctly and after submiting the form should be available in the same model as all other properties.

所以我的问题是,在将部分视图发送回去的动作中,我是否应该实例化该模型的新实例?但是然后数据将在模型的2个实例之间分割吗?

So my question, in the action which send the partial view back, should i really instantiate a new instace of the model? But then the data will be split between 2 instances of the model no?

如何安排呢,然后如何从局部视图将数据分配给一般视图模型?

How to arrange that, how can i then ass the data to the general views model from partial view?

推荐答案

我没有收到您的问题,但是您可以使用 HttpGet HttpPost 具有相同的名称(但签名不同,因为它们毕竟是方法),例如

i didnt get your question but you can annotate the ActionResults with HttpGet and HttpPost having same names (but different signatures, because they are methods after all) like

 [HttpGet]
 [ChildActionOnly]
    public ActionResult LocationGroup()
    {
        Model model = new Model();
        return PartialView("_TempView", model);
    }

在视图中,您必须执行

@model YOURMODELNAME
@using(Html.BeginForm("LocationGroup","Controller",FormMethod.POST)){
 @Html.TextBoxFor(x=>x.UserName)
 @Html.TextBoxFor(x=>x.Birthdate )
 <input type="submit" value="submit" />
}

现在定义一个帖子类型ActionResult

now define a post type ActionResult

 [HttpPost]
 [ChildActionOnly]
public ActionResult LocationGroup(YOUR_MODEL_TYPE model)
{
    if(ModelState.IsValid){
     //do something
    }
}

默认值模型绑定器将查看HttpContext,以查找所发布的值名称与模型属性之间的匹配项,并自动绑定该值

the default model binder will look into the HttpContext for the match between the posted value names and the properties of your model and bind the value automatically

这篇关于返回部分视图和模型的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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