- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 ASP.NET MVC Core 项目的以下代码中,Get 操作方法 Test(...)
显示年份的下拉列表。默认情况下,下拉列表将列表中的第一年(即 2000 年)显示为 Selected value
。如果我想在与选定值相同的列表中显示特定年份,我该如何实现 from action method
Test(...)
without 使用 ViewModel 技术? select 标签助手或 DropdownList html 助手的解决方案都可以 - 但所选值必须从操作方法中设置。
简而言之,您如何在此列表中将特定年份设置为选定值:ViewBag.YearsList = Enumerable.Range(2000, 15).Select(g => new SelectListItem { Value = g.ToString(), Text = g.ToString() }).ToList();
模型:
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions<BloggingContext> options)
: base(options)
{ }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public IList<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int PostYear { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
Controller :只显示相关的操作方法。
public class BlogsController : Controller
{
private readonly BloggingContext _context;
public BlogsController(BloggingContext context)
{
_context = context;
}
// GET: Blogs
public async Task<IActionResult> Index()
{
return View(_context.Blogs.ToList());
}
// GET: /Blogs/Test
[HttpGet]
public async Task<IActionResult> Test(string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
ViewBag.YearsList = Enumerable.Range(2000, 15).Select(g => new SelectListItem { Value = g.ToString(), Text = g.ToString() }).ToList();
//return View(await _context.Blogs.Include(p => p.Posts).ToListAsync());
var qrVM = from b in _context.Blogs
join p in _context.Posts on b.BlogId equals p.BlogId into bp
from c in bp.DefaultIfEmpty()
select new BlogsWithRelatedPostsViewModel { BlogID = b.BlogId, PostID = (c == null ? 0 : c.PostId), Url = b.Url, Title = (c == null ? string.Empty : c.Title), Content = (c == null ? string.Empty : c.Content) };
return View(await qrVM.ToListAsync());
}
// POST: /Blogs/Test
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Test(IList<BlogsWithRelatedPostsViewModel> model, string GO, int currentlySelectedIndex, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
ViewBag.YearsList = Enumerable.Range(2000, 15).Select(g => new SelectListItem { Value = g.ToString(), Text = g.ToString() }).ToList();
if (!string.IsNullOrEmpty(GO))
{
var qrVM = from b in _context.Blogs
join p in _context.Posts on b.BlogId equals p.BlogId into bp
from c in bp.DefaultIfEmpty()
where c == null? true : c.PostYear.Equals(currentlySelectedIndex)
select new BlogsWithRelatedPostsViewModel { BlogID = b.BlogId, PostID = (c == null ? 0 : c.PostId), Url = b.Url, Title = (c == null ? string.Empty : c.Title), Content = (c == null ? string.Empty : c.Content) };
return View(await qrVM.ToListAsync());
}
else if (ModelState.IsValid)
{
foreach (var item in model)
{
var oPost = _context.Posts.Where(r => r.PostId.Equals(item.PostID)).FirstOrDefault();
if (oPost != null)
{
oPost.Title = item.Title;
oPost.Content = item.Content;
oPost.PostYear = currentlySelectedIndex;
oPost.BlogId = item.BlogID; //according to new post below the blogId should exist for a newly created port - but just in case
}
else
{
if (item.PostID == 0)
{
Post oPostNew = new Post { BlogId = item.BlogID, Title = item.Title, Content = item.Content, PostYear = currentlySelectedIndex }; //need to use currentlySelectedIndex intead of item.FiscalYear in case of adding a record
_context.Add(oPostNew);
}
}
}
await _context.SaveChangesAsync();
//return RedirectToLocal(returnUrl);
return View(model);
}
// If we got this far, something failed, redisplay form
return View();
}
}
Test.cshtml View
@model IList<ASP_Core_Blogs.Models.BlogPostViewModels.BlogsWithRelatedPostsViewModel>
<div class="row">
<div class="col-md-12">
<form asp-controller="Blogs" asp-action="Test" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post">
@{
IEnumerable<SelectListItem> yearsList = (IEnumerable<SelectListItem>)ViewBag.YearsList;
var currentlySelectedIndex = 0; // Currently selected index (usually will come from model)
}
<strong>Select a Post Year</strong>
<h6>Choose a year and a URL to begin:</h6>
<label>Year:</label><select asp-for="@currentlySelectedIndex" asp-items="yearsList"></select><input type="submit" class="btn btn-default" name="GO" value="GO" />
<table class="table">
<thead>
<tr>
<th></th>
<th></th>
<th>Url</th>
<th>Title</th>
<th>Content</th>
</tr>
</thead>
<tbody>
@for (int i=0; i< Model.Count(); i++)
{
<tr>
<td>@Html.HiddenFor(r => r[i].BlogID)</td>
<td>@Html.HiddenFor(r => r[i].PostID)</td>
<td>
@Html.TextBoxFor(r => r[i].Url)
</td>
<td>
@Html.TextBoxFor(r => r[i].Title)
</td>
<td>
@Html.TextBoxFor(r => r[i].Content)
</td>
</tr>
}
</tbody>
</table>
<button type="submit" class="btn btn-default">Save</button>
</form>
</div>
</div>
最佳答案
因为你想避免使用 View 模型,你不能使用 DropDownListFor
但你可以使用 DropDownList
@Html.DropDownList("fieldname", new SelectList(ViewBag.YearsList, ViewBag.SelectedYear))
关于c# - ASP.NET CORE MVC 选择标签助手 - 如何在不使用 ModelView 的情况下设置所选值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39648592/
拜托,您对以下场景有什么建议(就架构质量而言)最好的方法: ModelViewA(父) - 需要从 ModelViewB 收集来显示数据 - 即使集合保持不变,也可以多次访问集合 ModelViewB
在 OpenGL 中,如何读取模型 View 矩阵中的当前 x/y 转换?我知道您必须将当前矩阵加载到数组中并从那里读取 float ,但我不知 Prop 体该怎么做。 最佳答案 为了检索当前的模型
flask 初学者在这里。我有一个使用以下模型的小 Flask 应用程序: class Question(db.Model): __tablename__ = 'questions'
更新了三种有效的方法,以及原来的一种无效的方法 我做了一个 Angular js 指令,我正在尝试访问 ctrl.$modelValue。它在主流中不起作用。 我有三种可能的解决方案,它们都有缺点。
我有一个被多个 View 模型使用的模型,并且我需要一些其他 javascript 组件来更新模型,由我的虚拟机观察到。我不知道如何做到这一点,因为在教程中,他们在 View 模型中“混合”模型。 这
这更像是一个一般的 Python 问题,而不是 Flask 问题。 这段代码来自https://github.com/mitsuhiko/flask/blob/master/flask/views.p
我正在研究 Flask-Admin 与 PeeWee 后端模型 View 的结合(但我的问题也可能适用于 SQLAlchemy 后端),有两件事我在文档或示例中找不到: (1)。当我的模型有一个唯一字
如何将 ModelView 上的字段设置为只读? class MyModelView(BaseModelView): column_list = ('name', 'last_name', '
目前在我的应用程序中,我使用 func/lambda 方法显示消息框,如下面的 url 中所述: http://www.deanchalk.me.uk/post/WPF-MVVM-e28093-Sim
如上所述here您可以通过声明 AdminIndexView 的子类并将 View 传递给管理应用程序来覆盖 Flask-Admin 的默认 View 。是否可以使用 ModelView 类 View
我正在使用带有flask-admin的模型 View ,并且我想在编辑/创建 View 中过滤列。列/字段是一种关系,我只想显示属于登录用户的字段,即 relationship_id == user.
我必须在其中创建一个简单的图像编辑器来执行翻转、翻转、添加边框等操作。我正在使用 jsp servlet。每当执行操作时,POST ajax 请求都会发送到服务器,其中包含数据-图像服务器相对路径和操
我正在练习 Django 的基于类的 View 。 在使用通用 CreateView 进行练习时,我很难理解为什么我的“fields”属性不起作用...我正在尝试使用 CreateView 构建一个
我想在模型 View 中为金额(自定义字段)添加过滤器 class FarmerAdmin(ModelView): column_descriptions = dict(amount = "A
我有一个使用 MVVM 模式 (MVVM Light Toolkit) 在 WPF 中开发的应用程序。 到目前为止,我没有遇到任何问题,直到需要在运行时更改与我的一些控件(一组 MenuItem)关联
获取 flask-admin ModelView URL 的正确方法是什么? 这是一个非常简单的例子: my_admin_view.py from flask.ext.admin.contrib.sq
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 8年前关闭。 Imp
我一直在四处寻找如何将模型 View 绑定(bind)到 View ,但我似乎无法解决。 我有一个名为 Search 的 View ,我想将它绑定(bind)到 SearchModelView。 Vi
我正在编写一个体积渲染程序,它不断调整一些平面几何形状,使其始终面向相机。每当相机旋转时,平面几何体就会旋转,以便看起来好像它相对于场景中的其他所有东西都没有移动。 (我使用相机的观看方向作为这些平面
我已经开始学习 MVC3 开发,但遇到了一些障碍。在我的项目中,我已将默认的 aspnet_ 成员表移动到单独的 SQL Server 2008 Express 数据库中。然后我添加了我自己的表来保存
我是一名优秀的程序员,十分优秀!