1

Suppose I have the following input control in a form

<form>
<input type="text" id="login-username" name="login-username" placeholder="Username">
</form>

How can i read the value of this control in an MVC control, however we cannot use dashes in variable names.

Any suggestions ??

mxmissile
  • 11,464
  • 3
  • 53
  • 79
Yaser
  • 99
  • 1
  • 1
  • 2

1 Answers1

1

I will suggest that you use an underscore like login_username, so that you can take advantage of model binding while passing data from the view to the controller.

Meanwhile you cannot declare the below code as a class attribute

public string login-username { get; set; }

NOTE

if you must use the dash, then consider using IFormCollection in ASP.NET core or FormCollection in ASP.NET

[HttpPost]
public IActionResult Index(IFormCollection collection)
{
    var login_username = collection["login-username"];
    //Other logic here
    return View();
}

Also you can still make use of the model binding but with extra lines of code and some other stuff see this

Bosco
  • 1,536
  • 2
  • 13
  • 25