Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String as a Model

Tags:

I thought this should have been an easier task :

Edit:

It seems till this day Asp.Net MVC couldn't provide a neat solution on this case:

If you want to pass a simple string as a model and you don't have to define more classes and stuff to do so... Any ideas ??

Pass simple string as a model


here I'm trying to have a simple string model.

I'm getting this error :

"Value cannot be null or empty" / "Parameter name: name"  

The View :

@model string @using (Html.BeginForm()) {          <span>Please Enter the code</span>          @Html.TextBoxFor(m => m) // Error Happens here         <button id="btnSubmit" title="Submit"></button> } 

The Controller :

public string CodeText { get; set; }  public HomeController() {     CodeText = "Please Enter MHM"; }  [HttpGet] public ActionResult Index() {     return View("Index", null, CodeText); }  [HttpPost] public ActionResult Index(string code) {     bool result = false;     if (code == "MHM")         result = true;      return View(); } 
like image 921
LastBye Avatar asked May 31 '13 08:05

LastBye


People also ask

What is the string model?

In particle physics, the Lund string model is a phenomenological model of hadronization. It treats all but the highest-energy gluons as field lines, which are attracted to each other due to the gluon self-interaction and so form a narrow tube (or string) of strong color field.

What is the difference between FromBody and FromQuery?

[FromQuery] - Gets values from the query string. [FromRoute] - Gets values from route data. [FromForm] - Gets values from posted form fields. [FromBody] - Gets values from the request body.

What is a model binder?

Model binding is a well-designed bridge between the HTTP request and the C# action methods. Data from HTTP requests are used by controllers and Razor pages. Route data, for example, may serve as a record key, while posted form fields may serve as values for model properties.

What is a model in C#?

In MVC M stands for Model and Model is a normal C# class. Model is responsible for handling data and business logic. A model represents the shape of the data. Model is responsible for handling database related changes.


2 Answers

There's a much cleaner way of passing a string as a model into your view. You just need to use named parameters when returning your view:

[HttpGet] public ActionResult Index() {     string myStringModel = "I am passing this string as a model in the view";     return View(model:myStringModel); } 
like image 84
Pete Avatar answered Nov 08 '22 13:11

Pete


I know you've already accepted an answer here - I'm adding this because there's a general gotcha associated with using a string model.

String as a model type in MVC is a nightmare, because if you do this in a controller:

string myStringModel = "Hello world"; return View("action", myStringModel); 

It ends up choosing the wrong overload, and passing the myStringModel as a master name to the view engine.

In general it is easier simply to wrap it in a proper model type, as the accepted answer describes, but you can also simply force the compiler to choose the correct overload of View() by casting the string to object:

return View("action", (object)myStringModel); 

The other issue you're having here of using TextBoxFor having issues with an 'unnamed' model - well you shouldn't be surprised by that... The only reason to use TextBoxFor is to ensure the fields are named correctly for binding when the underlying value is a property on a model type. In this case there is no name, because you're passing it as a top-level model type for a view - so you it could be argued that you shouldn't be using TextBoxFor() in the first place.

like image 41
Andras Zoltan Avatar answered Nov 08 '22 14:11

Andras Zoltan