I have a viewModel containing a list and another class like so :
public class MyViewModel
{
public List<Guids> Ids{ get; set; }
public History HistoryRecord{ get; set; }
}
public class History
{
public string UserName{ get; set; }
public string Email{ get; set; }
}
and a controller action that I need to send the above data to:
public async Task<IActionResult> myAction([Bind("Ids","UserName","Email")] MyViewModel viewModel)
{
...
}
i'm getting the values for userName and Email via inputs
<input type="text" name="name" id="nameTextbox" placeholder="name" required />
<input type="text" name="email" id="emailTextbox" placeholder="email" required />
how can I send this from a razor page?
i've tried using url.Action but this doesnt seem to work and not sure how to put multiple values in
<button type="submit" id="confirm" onclick="location.href='@Url.Action("myAction","myController", values: new {Ids= @Model.Ids.ToList(), Name = ? , Emil = ? })'">confrim</button>
CodePudding user response:
For binding values from Razor View to action you can use the asp-for tag helper. In your example:
<input asp-for="UserName" type="text" name="name" id="nameTextbox" placeholder="name" required />
<input asp-for="Email" type="text" name="email" id="emailTextbox" placeholder="email" required />
Your button code replace with:
<input type="submit" value="confrim" />
Don't forget to put this code into form tag and set [HttpPost] attribute for action
Final Razor View be like:
@model MyViewModel
<form asp-action="myAction" method="post">
<input asp-for="@Model.Ids[i]" required />
<input asp-for="@Model.UserName" type="text" name="name" id="nameTextbox" placeholder="name" required />
<input asp-for="@Model.Email" type="text" name="email" id="emailTextbox" placeholder="email" required />
<input type="submit" value="confrim" />
</form>
More information on link.
CodePudding user response:
You can try to add hidden inputs to the form,It looks like you want to use List<Guid> Ids
,here is a demo:
Model:
public class MyViewModel
{
public List<Guid> Ids{ get; set; }
public History HistoryRecord{ get; set; }
}
add following data to the form:
@for(var i=0;i<Model.Ids.Count;i )
{
<input asp-for="@Model.Ids[i]" hidden />
}