【问题标题】:How to get the View to return the ID via a ViewModel to the controller?如何让视图通过 ViewModel 将 ID 返回给控制器?
【发布时间】:2017-10-11 19:19:29
【问题描述】:

我目前有一个视图,它通过TechnicianViewModel 返回技术人员的IEnumerable 集合。视图模型填充良好并正确显示对象。

但是,目前我需要有人为我指明选择特定技术人员的正确方向。

如果我在技术人员上单击More Info,将启动[HttpGet] 请求,这将导致查询字符串。而这正是违反本项目要求的。(项目要求:URL中不应出现查询字符串)

这是视图:-

  @model IEnumerable<ActionAugerMVC.ViewModels.TechnicianViewModel>
  <div class="row">
        @foreach (var item in Model)
        {
            <div class="col-sm-6 col-md-3">
                <div class="">
                    <div class="">
                        <div class="">
                           @item.UserName
                        </div>
                        <div class="">
                            Technician
                        </div>
                    </div> 
                    <div class="">
                        <img src="~\imagesurl-@item.GenerateName()" class="img-responsive" alt="...">
                    </div>
                    <div class="">
                        <p class="overlay__desc">
                           @item.Bio
                        </p>
                        <ul class="overlay__social">
                            <li><a href="#"><i class="ion-social-twitter"></i></a></li>
                            <li><a href="#"><i class="ion-social-facebook"></i></a></li>
                            <li><a href="#"><i class="ion-social-skype"></i></a></li>
                            <li><a href="#"><i class="ion-social-whatsapp-outline"></i></a></li>
                        </ul>
                        <a href="@Url.Action("Profile","Technician",new { url = item.GenerateURL() })" class="btn btn-accent">More info</a>


                    </div> 
                </div> 
            </div>
        }
        </div> <!-- / .row -->

这是视图模型:-

 public class TechnicianViewModel
 {
 public Guid ID { get; set; }
 public string UserName { get; set; }
 public string Bio { get; set; }
 public string GenerateName()
 {
    string str = GenerateURL();
    str = str + ".jpg";
    return str;
 }
 public string GenerateURL()
 {
    string phrase = string.Format("{0}", UserName);
    string str = phrase.ToLower();
    str = Regex.Replace(str, @"\s", "-");
    return str;
 }
}

我怎样才能避免我的控制器方法成为[HttpGet],就像我在这里实现的那样,以便我可以从视图返回的视图模型对象中获得 ID。

        [HttpGet]
    [Route("technician/profile/calgary-{url}")]
    public IActionResult Profile(Guid? ID,string url)
    {
        var Profile = unitOfWork.TechnicianRepository.GetByGuidId(ID);
        return View(Profile);
    }

【问题讨论】:

  • 如果您想访问模型属性而不将它们作为路由值发送,您需要使用提交元素发布表单,或通过 jquery 执行提交。
  • @akerra 所以我必须将当前包含Url.Actiona 链接更改为表单发布按钮?
  • 是的,我会推荐类似 '更多信息' 但是你需要一些方法来了解哪个技术人员被选中,因此您可能需要将隐藏 div 的值设置为所选技术人员的 id 并将其绑定到新的模型字段
  • 隐藏的 div 可能会映射到 TechnicianViewModel 的 ID 属性。我可以使用该 ID 从 Technician 模型中检索整个对象
  • 如果我这样做,我不会使用 IEnumerable 而是让模型包含 List 字段和 SelectedTechnicianId 字段(这将是隐藏的 div 值)跨度>

标签: c# razor asp.net-core-mvc


【解决方案1】:

在您的视图中,将链接更改为以下内容:

<a href="@Url.Action("Profile","Technician",new { id = item.ID.ToString() })" class="btn btn-accent">More info</a>

您可以摆脱控制器方法上的 HttpGet 和 Route 装饰器。这些将由默认路由配置处理:

public ActionResult Profile(string id)
{
    TempData["guid"] = id;
    return RedirectToAction("Profile");
}

public ActionResult Profile()
{
    Guid guid = Guid.Parse(TempData["guid"]);
    var Profile = unitOfWork.TechnicianRepository.GetByGuidId(guid);
    return View(Profile);
}

【讨论】:

  • 这会阻止 ID 出现在 URL 中吗?
  • 为什么不想要 URL 中的 ID?
  • 尝试上面的方法,它使用 TempData 在清理 URL 时传递 guid
  • 因为这是经理要求我做的。他不想在 url 中看到任何问号。我建议他无论如何这不会影响谷歌列表。然而,这是我目前的情况,这是我被要求遵守的要求。
  • 如果他只担心问号,传递id作为参数不会加一个,因为它是一个注册的路由配置。该网址看起来像 .../Technician/Profile/1233-12312-4322-4322-2342。但是我提供的编辑后的答案应该看起来像 .../Technician/Profile。注意我没有测试过。
猜你喜欢
  • 1970-01-01
  • 2014-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 2022-07-28
相关资源
最近更新 更多