0

First of all, I'm still a beginner in MVC

Obviously anyone knows that we can pass by /CONTROLLER/METHOD/ID, but in some cases, like I need to pass 3-4 params into the controller, how should I do? Is there a good way of passing them all?

The code below is a controller dealing with post request, why I cannot use the "temp1"? it said that one should be declared before use, what that means?

[HttpPost]
public ActionResult Payment_Home(Wires_SWIFT temp1){
    string str = temp1.BENEFICIARY_NAME;
    DbQuery<Wires_SWIFT> dbq = db.Wires_SWIFT.Where(d => d.BENEFICIARY_NAME LIKE temp1.);
    return View();
}
3
  • Whether temp1 contains a value or not depends on the structure of what a Wires_SWIFT is and what values are being submitted via the form post from the page. The names and implicit types of those values should match the properties on a Wires_SWIFT object. Commented Jul 16, 2013 at 15:00
  • @Oded♦ Made great sense! but is there any way that I can get the string value in temp1? temp1.BENEFICIARY_NAME.ToString() doesn't help Commented Jul 16, 2013 at 15:24
  • @LifeScript - It is probably empty - you need to setup the values of post action parameters to have the same names as in the form posting them. This article may be of help: simple-talk.com/dotnet/asp.net/… Commented Jul 16, 2013 at 15:26

2 Answers 2

2

Use a ViewModel.

This is a class that contains all the values you need - you populate it in the controller action and pass it in to the view (helps if the view is strongly typed to the model.

public class MyModel
{
  public string SomeValue { get; set; }
  public string SomeOtherValue { get; set; }
}

// controller
var myModel = new MyModel...
return View(myModel);

// view
@model MyModel

@Model.SomeValue
Sign up to request clarification or add additional context in comments.

1 Comment

so how I gonna write my form? Like @Html.TextBoxFor(b => b)?
0

You need to make sure you have a route set correctly.

so to match dothis/action/param1/param2 you'd want a route like:

routes.MapRoute(
    "MyRoute", // Route name
    "{controller}/{action}/{param1}/{param2}", // URL with parameters
new { controller = "Surveys", action = "Index", param1 = "param1", param2 = "param2" }  // Parameter defaults
);

For the above route to actually work, you'll need a controller and action such as

public class DoThisController : Controller
....
public ActionResult Action(int param1, int param2)
....

The type of the action parameters doesn't matter, but the name should match the name defined in the rule to allow the action to be located by ASP.Net

In your case, the error is because your parameter is called temp1 & in the route data there is nothing called that.

1 Comment

a very quick question: how can I make it as regular html text between to razor markup tag? for example: \@Html.str hello \@Html.str2 How can I make "hello" as html output?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.