在ASP MVC4中传递模型背后发生了什么 [英] what happens behind model passing in ASP MVC4

查看:54
本文介绍了在ASP MVC4中传递模型背后发生了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在是我学习MVC的第三周,现在就学习ASP MVC

Learning the ASP MVC now, just my 3rd week on MVC

我对建模过程进行了一些测试,基本上控制器只是获取了模型,然后不执行任何操作就进入了视图,但是似乎代码失败了.

I did some tests on modeling pass, basically the controller just get the model, and pass into the view without doing anything, but it seems like the code failed.

下面是我创建的ViewModel

below is the ViewModel I created

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Bank2.Models.ViewModel
{
    public class PaymentView
    {
        public List<Wires_SWIFT> lists{get; set;}
        public string b_str{get; set;}
        public string o_str{get; set;}
    }
}

这是视图:

@model ViewModel
@using(Html.BeginForm("Payment","Home",FormMethod.Post)){

        @Html.TextBoxFor(d=> d.o_str)<br/>
        @Html.TextBoxFor(d=> d.b_str)<br/>
        <input type="submit" name="Search">
}

控制器获取模型并将其立即传递

The controller grabs the model and pass it through right away

...
[HttpPost]
public ActionResult Payment(ViewModel m){
   return View(m)
}

...

我在texboxes中键入了两个字符串:"aa"和"bb",在我单击提交后,它们应该在那儿,因为相同的对象被传回了,但是该字段现在为空

I typed two strings in texboxes: like "aa" and "bb", after I clicked the submit, they supposed to be there because the same object being passed back, but the field is empty now

我是否错过了有关建模传递的重要事项?任何建议都欢迎

Did I miss something important about modeling passing? Any kind of suggestions are welcomed

推荐答案

您需要具有ViewModel的getter和setter才能从已发布的表单中检索值.

You need to have getter and setter for ViewModel in order to retrieve the values from posted form.

public class ViewModel
{
    public string str1 { get; set; }
    public string str2 { get; set; }
    public List<int> list { get; set; }
}

这篇关于在ASP MVC4中传递模型背后发生了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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