1

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

LifeScript
  • 1,116
  • 5
  • 15
  • 24
  • Are you getting any errors? What is the expected behaviour? – Charlie Brown Aug 05 '13 at 17:08
  • @Charlie Brown after I press the submit button, all the values supposed to be there right? caz in the controller the model's been passed back into a view, yet the textbox are empty now – LifeScript Aug 05 '13 at 17:11
  • It depends on how you have the GET-REDIRECT loop implemented, what does your GET action look like? – Charlie Brown Aug 05 '13 at 17:15
  • @Charlie Brown Instead, I used a post method, does that matter when dealing model? - just curious – LifeScript Aug 05 '13 at 17:25
  • The ViewModel code is not even *close* to being compilable, so since it apparently *does* compile, you're not showing us the actual code for it. Which is the most important part, since the View and Action as written aren't what's causing the problem here. – JimmiTh Aug 05 '13 at 17:32
  • @JimmiTh I just copy and paste my original code, would you like to help me with that? be free to point out if something wrong in ViewModel – LifeScript Aug 05 '13 at 17:40
  • Well, what *was* wrong before your edit was that `ViewModel { }` isn't compilable - you need to declare it as a class or struct. And `public str1;` isn't compilable either - it needs a type. It's hard to find problems in a piece of code, if you don't actually post the code (or code equivalent to it) that has problems. ;-) – JimmiTh Aug 05 '13 at 17:44
  • @JimmiTh btw, do you have any ASP MVC text book recommendations? – LifeScript Aug 05 '13 at 18:57
  • Well I m new to MVC but you have a line in the view `@model ViewModel`. I have never seen passing `namespaces` in the cshtml file. I think it should be `Models.ViewModel.PaymentView` correct me if I am wrong. – shashwat Aug 08 '13 at 02:36
  • @harsh Sorry I should copy all my code, that is actually the class name, because I included the "using folder.folder2.folder3", so each time I can use the class directly without path. – LifeScript Aug 08 '13 at 13:33

2 Answers2

5

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; }
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • 1
    Yup this is your answer and I'm sorry to admit I totally missed you didn't have getters and setters. You basically defined fields in your model not properties. Nice catch @Win. – Marko Aug 05 '13 at 17:29
  • @Win Oh my god you made it!!! Now everything working well! Good point!! =] before I check that, I just have to ask: what's the real difference between with and without "{get;set;}"? If I write like that, they just generate you a default getter/setter, but why that makes a difference? – LifeScript Aug 05 '13 at 17:48
  • @Charlie Brown so that means from now on every class it never hurt to add getter/setter behind them right? – LifeScript Aug 05 '13 at 17:50
  • 1
    @LifeScript : "what's the real difference between with and without {get;set;}?" - The difference is that without them, you're not declaring a property, but a field. And one difference between properties and fields when it comes to MVC is that the HTML Helpers won't look at fields at all, when populating the value of your `` elements - only properties (the model binders won't look at fields either, when populating your model from the POST data). – JimmiTh Aug 05 '13 at 17:54
  • @JimmiTh Good point, so if I understood it right, in a class, field is like some pre-declare variables that may use in function, property is like member stays in the class, am i right? – LifeScript Aug 05 '13 at 18:07
  • @LifeScript: See http://stackoverflow.com/questions/6542827 and http://stackoverflow.com/questions/653536. And add to the answers there that the CLR (rightly) treats them as entirely different concepts - just as different as methods vs. fields - and that MVC only looks at properties on your models and ignores fields. – JimmiTh Aug 05 '13 at 18:16
  • 2
    If your getting started in .net, I recommend reading the framework guidelines. http://msdn.microsoft.com/en-us/library/ms229042.aspx – Charlie Brown Aug 05 '13 at 18:26
  • @Charlie Brown great! that's exactly what I want, besides, do you have any text book recommendations? – LifeScript Aug 05 '13 at 18:56
  • 2
    JimmiTh and Charlie pretty much answer the question. ***Appreciated both of your help!*** For the books, I personally read and like those two books regarding C# and MVC - [Pro C# 5.0](http://www.amazon.com/Pro-5-0-NET-4-5-Framework/dp/1430242337/ref=sr_1_3?ie=UTF8&qid=1375729068&sr=8-3&keywords=c%23) and [Pro ASP.Net MVC4](http://www.amazon.com/Pro-ASP-NET-MVC-Adam-Freeman/dp/1430242361/ref=sr_1_1?ie=UTF8&qid=1375729180&sr=8-1&keywords=mvc+4). – Win Aug 05 '13 at 19:01
  • @LifeScript The suggestions by Win are a great place to start – Charlie Brown Aug 05 '13 at 19:14
  • @Win Thank you so much for making my day a better one!!! I really appreciate your help and time on me, thanks for being my hero! – LifeScript Aug 05 '13 at 19:53
  • @Charlie Brown you got me! thanks for your help, I really enjoyed your suggestion! thank you for your kind contribution!!!! – LifeScript Aug 05 '13 at 19:54
0

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.

Andy T
  • 10,223
  • 5
  • 53
  • 95