LifeScript
LifeScript

Reputation: 1114

what happens behind model passing in ASP MVC4

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.

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;}
    }
}

This is the View:

@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)
}

...

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

Upvotes: 1

Views: 261

Answers (2)

Win
Win

Reputation: 62260

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; }
}

Upvotes: 5

Andy T
Andy T

Reputation: 9881

You are showing us the POST-version of the method. Usually, the post action method would take the model, do some kind of processing (input validation, save to db, call a service, etc.), then if success, redirect to a different page.

You would usually only call the view from the POST action method if you have any problems with the inputs that require the user to fix.

Upvotes: 0

Related Questions