I'm having a hard time googling this, but I can't find my issue and I find it weird as it seems this should be something really simple (Maybe i'm using bad keywords)...
Anyway I have an ASP.NET Application And I would like to pass a string parameter to a cshtml view.
From My index.cshtml I pass the parameter I want:
<td>
@Html.ActionLink("Crear saludo", "Create", new { nombreSeguidor = item })
</td>
This is the part where I'm lost... It receives the parameter correctly, but I'm unable to make use of the string on the Create.cshtml.
// GET: Saludos/Create
public ActionResult Create(string nombreSeguidor)
{
ViewBag.nombreSeguidor = nombreSeguidor; //This is my try on achieving the behaviour I want
ViewBag.Seguidores_Id = new SelectList(_repositorioSeguidores.DameTodo(), "Id", "NombreTwitch");
return View();
}
It seems to receive Seguidores_Id correctly, but not my new parameter (The ViewBag.Seguidores_Id was create by Visual Automatically)
Thanks in advance
Edit:
Thxs for the reply:
It seems that the first answer was What I was looking for, but now I have other issues.
My intention on this view is to create an item with a model, but some of the variables will be created automatically. This is the model I have:
public int Id { get; set; }
public string Saludo { get; set; }
public string CreadoPor { get; set; }
public Nullable<int> Seguidores_Id { get; set; }
Id will be auto; CreadoPor will be an unique string if created this way (always the same); and Seguidores_Id will depend on the parameter "nombreSeguidor" We have passed.
So, in this view, the only variable I want to be editable is Saludo
How do I fill other variables? Until now, I was using this autogenerated-code:
@model string
@*@model TwitchWebApi.Models.Saludos*@
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div >
<h4>Saludos</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@*@Html.HiddenFor(model => model.Seguidores_Id)*@
<p>
Crear saludo para @Model
</p>
<div >
@Html.LabelFor(model => model.Saludo, htmlAttributes: new { @class = "control-label col-md-2" })
<div >
@Html.EditorFor(model => model.Saludo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Saludo, "", new { @class = "text-danger" })
</div>
</div>
<div >
@Html.LabelFor(model => model.CreadoPor, htmlAttributes: new { @class = "control-label col-md-2" })
<div >
@Html.EditorFor(model => model.CreadoPor, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CreadoPor, "", new { @class = "text-danger" })
</div>
</div>
<div >
@Html.LabelFor(model => model.Seguidores_Id, "Seguidores_Id", htmlAttributes: new { @class = "control-label col-md-2" })
<div >
@Html.DropDownList("Seguidores_Id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Seguidores_Id, "", new { @class = "text-danger" })
</div>
</div>
CodePudding user response:
If you want to pass a string parameter from the public ActionResult Create(string nombreSeguidor)
action method to the Create.cshtml
view it is possible just make it like below:
// GET: Saludos/Create
public ActionResult Create(string nombreSeguidor)
{
ViewBag.Seguidores_Id = new SelectList(_repositorioSeguidores.DameTodo(), "Id", "NombreTwitch");
return View((object)nombreSeguidor);
}
In the Create.cshtml
:
@model string
<!DOCTYPE html>
<html>
<body>
<div>
The passed string: @Model
</div>
</body>
</html>
Even preferable to use the strongly typed view instead of using ViewData
or ViewBag
.