Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: How to 'model bind' a non html-helper element

Could you tell me a way(s) that I can bind a model property to a html-element, created without using html helper?

In other words to a plain html element such as: <input type="text" />

like image 446
dan Avatar asked Jan 16 '13 07:01

dan


People also ask

How do you bind the data model in MVC?

Step 1: Open the VS 2019 and select the ASP.NET core web application. Step 2: Select the template of the web application (MVC). Step 3: Go to the home controller and student basic type binding. Step 5: Now, press f5 and run the solution.

How do you bind a model in HTML?

So look at the markup you want to generate. You should have some List property in your model you want to bind the drop down list to.. So the name attribute of the Select is the Name of the Property in the Model.

What is @html in MVC?

What is Inline HTML Helper in MVC 5? Inline HTML Helper is used to create a reusable Helper method by using the Razor @helper tag. Inline helpers can be reused only on the same view. We can not use Inline helper to the different view Pages. We can create our own Inline helper method based on our requirements.


2 Answers

If you are referring to Model Binding, it does not require helpers, but naming convention. Helpers just make it easy and concise to create the HTML markup.

You could create plain HTML inputs and just set the name attribute correctly. The default naming convention is just dot based, omitting the parent level entity's name, but qualifying it from there.

Consider this controller:

public class MyControllerController : Controller
{
     public ActionResult Submit()
     {
         return View(new MyViewModel());
     }

     [HttpPost]
     public ActionResult Submit(MyViewModel model)
     {
            // model should be not null, with properties properly initialized from form values
            return View(model);
     }
}

And this model:

public class MyNestedViewModel
{
    public string AnotherProperty { get; set; }
}

public class MyViewModel
{
    public MyViewModel()
    {
         Nested = new MyNestedViewModel();
    }

    public string SomeProperty { get; set; }

    public MyNestedViewModel Nested  { get; set; }
}

You could create the following form purely in HTML:

<form method="POST" action="MyController/Submit">
    <div><label>Some property</label><input type="text" name="SomeProperty" /></div>
    <div><label>Another property</label><input type="text" name="Nested.AnotherProperty" /></div>
    <button type="submit">Submit</button>
</form>

If you want to display the posted values (in the second Submit overload), your HTML will have to be modified render the model properties. You'd place this in a view, in this case using Razor syntax and called Submit.cshtml:

@model MyViewModel
<form method="POST" action="MyController/Submit">
    <div><label>Some property</label><input type="text" name="SomeProperty" value="@Model.SomeProperty" /></div>
    <div><label>Another property</label><input type="text" name="Nested.AnotherProperty" value="@Model.Nested.SomeProperty" /></div>
    <button type="submit">Submit</button>
</form>

So, this can be done without helpers, but you'd want to use them as much as possible.

like image 97
moribvndvs Avatar answered Sep 20 '22 21:09

moribvndvs


Just give it a name:

<input type="text" name="foo" />

and then inside your controller action simply have an argument with the same name:

public ActionResult Process(string foo)
{
    // The foo argument will contain the value entered in the 
    // corresponding input field
}
like image 36
Darin Dimitrov Avatar answered Sep 23 '22 21:09

Darin Dimitrov