任何机构都可以解释,何时使用
我有一个要求,我需要在控制器中设置一个值,控制器将重定向到控制器二,控制器二将呈现视图.
我试图使用ViewBag,当我到达Controller Two时,值会丢失.
我可以知道何时使用和优缺点?
谢谢
任何人都可以给我一个关于ModelState在Asp.net MVC中的角色的简洁定义(或者链接到一个).特别是我需要知道在什么情况下打电话是必要的或可取的ModelState.Clear().
有点开放了 ......对不起,我想如果告诉你我在做什么可能会有所帮助:
我在一个名为"Page"的控制器上有一个Edit of Action.当我第一次看到表单更改页面的详细信息时,所有内容都很好地加载(绑定到"MyCmsPage"对象).然后,我单击一个按钮,为MyCmsPage对象的某个字段(MyCmsPage.SeoTitle)生成一个值.它生成正常并更新对象,然后我返回动作结果与新修改的页面对象,并期望相关的文本框(使用渲染<%= Html.TextBox("seoTitle", page.SeoTitle)%>)更新...但是它显示了加载的旧模型的值.
我通过使用来解决它,ModelState.Clear()但我需要知道它为什么/如何工作所以我不只是盲目地做.
的PageController:
[AcceptVerbs("POST")]
public ActionResult Edit(MyCmsPage page, string submitButton)
{
// add the seoTitle to the current page object
page.GenerateSeoTitle();
// why must I do this?
ModelState.Clear();
// return the modified page object
return View(page);
}
Run Code Online (Sandbox Code Playgroud)
ASPX:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyCmsPage>" %>
....
<div class="c">
<label for="seoTitle">
Seo Title</label>
<%= Html.TextBox("seoTitle", page.SeoTitle)%>
<input type="submit" value="Generate Seo Title" name="submitButton" />
</div>
Run Code Online (Sandbox Code Playgroud) 我在MVC中有一个大(ish)形式.
我需要能够生成包含该表单子集中的数据的excel文件.
棘手的一点是,这不应该影响表单的其余部分,所以我想通过AJAX来做.我在SO上遇到了几个似乎有关的问题,但我无法弄清楚答案的意思.
这个似乎最接近我所追求的:asp-net-mvc-downloads-excel - 但我不确定我理解这个反应,现在已经有几年了.我还看到了另一篇关于使用iframe处理文件下载的文章(找不到了),但我不知道如何使用MVC.
我的excel文件返回正常,如果我正在做一个完整的帖子回来但我不能让它在mvc中使用AJAX.
我试图搞清楚MVC框架,所以请耐心等待.
现在,我唯一使用会话存储的是存储当前登录的用户.我的网站很简单.对于此示例,请考虑三个域对象:Person,Meeting和File.用户可以登录并查看会议的"仅限会员"个人资料,并可以向其添加文件,或查看会议的公共"个人资料"(如果他们未登录).
因此,从会议的私人资料中,登录用户,我有一个"添加文件"链接.此链接路由到FileContoller.Add(int meetingId).通过此操作,我获得了用户希望使用会议ID添加文件的会议,但在发布表单后,我仍然需要知道用户正在向哪个会议添加文件.这就是我的问题所在,我应该通过TempData传递"当前正在与之交互"会议,还是将其添加到会话存储中?
这就是我目前的添加操作设置,但它不起作用:
public ActionResult Add(int meetingId)
{
try
{
var meeting = _meetingsRepository.GetById(meetingId);
ViewData.Model = meeting;
TempData[TempDataKeys.CurrentMeeting] = meeting; /* add to tempdata here */
}
catch (Exception)
{
TempData[TempDataKeys.ErrorMessage] = "Unable to add files to this meeting.";
return RedirectToRoute("MeetingsIndex");
}
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add(FormCollection form)
{
var member = Session[SessionStateKeys.Member] as Member;
var meeting = TempData[TempDataKeys.CurrentMeeting] as Meeting; /* meeting ends up null here */
if (member == null)
{
TempData[TempDataKeys.ErrorMessage] = "You …Run Code Online (Sandbox Code Playgroud) 我们正在研究Kendo MVC UI,我们将数据从一个视图发送到另一个视图,所有数据(testbox,下拉列表)都被传递到除附件(pdf,xlsx)之外的下一个视图.
下面是我们编写的控制器中从代码中捕获并保存数据并将相同数据传递给另一个视图并将数据绑定到kendo控件的代码(也可以上传控件)
public ActionResult SaveData(System.Web.Mvc.FormCollection form, IEnumerable<HttpPostedFileBase> files) // insert operation
{
//*************************//
if (form != null)
{
string ddluserexceptioncategory = Convert.ToString(form["txtexceptioncategory"], CultureInfo.InvariantCulture);
if (!string.IsNullOrEmpty(ddluserexceptioncategory))
{
ddluserexceptioncategory = ddluserexceptioncategory.Trim();
}
if (ddluserexceptioncategory == "User Management")
{
//Bind the data to the class object(_clsObj)
if (files != null)
{
TempData["FileName"] = files;
_clsObj.Files = files;
}
TempData["SecondViewData"] = _clsObj;
return RedirectToAction("ExceptionType", "Home", new { id = 0, regionId = _clsObj.RegionId, status1 = "New,In Progress", keyword1 = string.Empty });
} …Run Code Online (Sandbox Code Playgroud) 我有一个要求,当用户成功发布一些表单数据时,我想要一个模式对话框来显示 POST 是否成功,以及将视图重置为空状态(如果成功)。
我该怎么办呢?
我的 POST 逻辑工作正常,但就目前情况而言,没有任何反馈表明操作是否成功。
在关于ASP.NET MVC的网络上的各种文章中,人们都提到微软的MVC实现在某种程度上与"经典"MVC模式不同.
但我从来没有见过任何人进入这个任何细节,乍一看我没有看到ASP.NET MVC是如何从所描述的内容,比方说,任何不同的位置.
所以我的问题是:ASP.NET MVC真的违反了任何MVC模式原则吗?
我需要将对象列表从一个控制器传递到另一个控制器.我已经阅读了类似问题的答案,但没有什么可以帮助我.这是第一个控制器的代码:
[HttpPost]
public ActionResult Admin_Mgmt(List<thing> things, int Action_Code)
{
switch (Action_Code)
{
case 1:
{ //redirecting requesting to another controller
return RedirectToAction("Quran_Loading", "Request_Handler",things);
}
default:
...
}
}
Run Code Online (Sandbox Code Playgroud)
Request_Handler代码:
public class Request_HandlerController : Controller
{
public ActionResult Quran_Loading(List<thing> thin)
{...}
}
Run Code Online (Sandbox Code Playgroud)
但问题是Quran_Loading方法中的列表为null.任何的想法 ?
您好,我正在尝试向其添加对象TempData并重定向到另一个控制器操作。使用时出现错误消息500 TempData。
public IActionResult Attach(long Id)
{
Story searchedStory=this.context.Stories.Find(Id);
if(searchedStory!=null)
{
TempData["tStory"]=searchedStory; //JsonConvert.SerializeObject(searchedStory) gets no error and passes
return RedirectToAction("Index","Location");
}
return View("Index");
}
public IActionResult Index(object _story)
{
Story story=TempData["tStory"] as Story;
if(story !=null)
{
List<Location> Locations = this.context.Locations.ToList();
ViewBag._story =JsonConvert.SerializeObject(story);
ViewBag.rstory=story;
return View(context.Locations);
}
return RedirectToAction("Index","Story");
}
Run Code Online (Sandbox Code Playgroud)
PS阅读可能的解决方案我已添加 app.UseSession()到Configure方法services.AddServices()中,ConfigureServices但方法无济于事。是否有任何我必须注意的模糊设置?
PS添加ConfigureServices和UseSession
配置服务
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddDbContext<TreasureContext>(x=>x.UseMySql(connectionString:Constrings[3]));
services.AddMvc();
services.AddSession();
}
Run Code Online (Sandbox Code Playgroud)
配置
public void Configure(IApplicationBuilder …Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×7
c# ×3
tempdata ×2
asp.net-core ×1
jquery ×1
kendo-ui ×1
modelstate ×1
session ×1
vb.net ×1