I would like to have a button where on click it changes a string value from null to any other text (for example 'yes'). I have a button in the view that calls an update method in the controller but I don't know what to put inside that update method.
我希望有一个按钮,点击它可以将字符串值从null更改为任何其他文本(例如'yes')。我在视图中有一个按钮,它调用控制器中的更新方法,但我不知道该更新方法中包含什么。
In the view, this is the button ('item' refers to model, 'Activated' refers to a string column in that model which has a default value of null)
在视图中,这是按钮('item'指模型,'Activated'指的是该模型中的字符串列,其默认值为null)
@model IEnumerable<ProjectMVC2.Models.ApplicationUser>
@foreach (var item in Model)
{
@if (item.Activated == null)
{
using (Html.BeginForm("Update", "User", new { id = item.Id })){
@Html.AntiForgeryToken()
<td class="button btn-default" align="center">
<input type="submit" value="Activate" class="btn btn-default" />
</td>
}
}
else if (item.Activated == "Yes")
{
<td class="c" align="center">
Activated already
</td>
}
I have the method in my controller
我的控制器中有方法
public class UserController : BaseController
{
ApplicationDbContext context;
public UserController()
{
context = new ApplicationDbContext();
}
// GET: User
public ActionResult Index()
{
var User = context.Users.ToList();
return View(User);
}
public ActionResult Update()
{
return RedirectToAction("Index")
}
}
In the method what should I put that will allow for the change to happen? Please if anyone can help, I will really appreciate it.
在该方法中,我应该放什么才能允许更改发生?如果有人可以提供帮助,我将非常感激。
2 个解决方案
#1
0
You'll have to updated your model itself:
您必须更新模型本身:
public ActionResult Update()
{
var users = context.Users.ToList();
foreach(var user in users)
{
user.Activated = "Yes";
}
context.SaveChanges();
return RedirectToAction("Index")
}
However, this changes are persisted so if you come back to the page again you'll still see user.Activated = "Yes"
. If you need something temporary, I would suggest to use following code.
但是,此更改会保留,因此如果您再次返回该页面,您仍会看到user.Activated =“是”。如果您需要临时的东西,我建议使用以下代码。
public ActionResult Update()
{
var users = context.Users.ToList();
foreach(var user in users)
{
user.Activated = "Yes";
}
return View("Index", users);
}
#2
0
Use a for
loop to take advantage of model binding to a collection.
使用for循环可以利用模型绑定到集合。
This is accomplished by using an indexing expression e.g. Model[i]
.
这是通过使用索引表达式来完成的。模型[i]中。
Add a HiddenFor
to tell the controller which users need to be activated.
添加HiddenFor以告诉控制器需要激活哪些用户。
@for (var i = 0; i < Model.Length; i++)
{
var item = Model[i];
@if (item.Activated == null)
{
using (Html.BeginForm("Update", "User", new { id = item.Id }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(_ => Model[i].Id)
<td class="button btn-default" align="center">
<input type="submit" value="Activate" class="btn btn-default" />
</td>
}
}
else if (item.Activated == "Yes")
{
<td class="c" align="center">
Activated already
</td>
}
The update method now accepts a collection of users to activate.
update方法现在接受要激活的用户集合。
public ActionResult Update(IEnumerable<ApplicationUser> usersToActivate)
{
// Are there any user to active?
if (usersToActivate == null)
{
return View();
}
var userIds = usersToActivate.Select(u => u.Id).ToList();
var usersToUpdate = context.Users.Where(user => userIds.Contains(user.Id));
foreach (var userToUpdate in usersToUpdate)
{
userToUpdate.activated = "Yes";
}
context.SaveChanges();
return RedirectToAction("Index")
}
#1
0
You'll have to updated your model itself:
您必须更新模型本身:
public ActionResult Update()
{
var users = context.Users.ToList();
foreach(var user in users)
{
user.Activated = "Yes";
}
context.SaveChanges();
return RedirectToAction("Index")
}
However, this changes are persisted so if you come back to the page again you'll still see user.Activated = "Yes"
. If you need something temporary, I would suggest to use following code.
但是,此更改会保留,因此如果您再次返回该页面,您仍会看到user.Activated =“是”。如果您需要临时的东西,我建议使用以下代码。
public ActionResult Update()
{
var users = context.Users.ToList();
foreach(var user in users)
{
user.Activated = "Yes";
}
return View("Index", users);
}
#2
0
Use a for
loop to take advantage of model binding to a collection.
使用for循环可以利用模型绑定到集合。
This is accomplished by using an indexing expression e.g. Model[i]
.
这是通过使用索引表达式来完成的。模型[i]中。
Add a HiddenFor
to tell the controller which users need to be activated.
添加HiddenFor以告诉控制器需要激活哪些用户。
@for (var i = 0; i < Model.Length; i++)
{
var item = Model[i];
@if (item.Activated == null)
{
using (Html.BeginForm("Update", "User", new { id = item.Id }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(_ => Model[i].Id)
<td class="button btn-default" align="center">
<input type="submit" value="Activate" class="btn btn-default" />
</td>
}
}
else if (item.Activated == "Yes")
{
<td class="c" align="center">
Activated already
</td>
}
The update method now accepts a collection of users to activate.
update方法现在接受要激活的用户集合。
public ActionResult Update(IEnumerable<ApplicationUser> usersToActivate)
{
// Are there any user to active?
if (usersToActivate == null)
{
return View();
}
var userIds = usersToActivate.Select(u => u.Id).ToList();
var usersToUpdate = context.Users.Where(user => userIds.Contains(user.Id));
foreach (var userToUpdate in usersToUpdate)
{
userToUpdate.activated = "Yes";
}
context.SaveChanges();
return RedirectToAction("Index")
}