如何处理MVC中的无效URL?
例如:当用户输入http:// localhost/User/MyProfile而不是 http:// localhost/User/Profile时,它将引发异常.
如何处理这个请求?
model-view-controller asp.net-mvc invalid-url asp.net-mvc-routing
是否有开发Linux控制台应用程序的框架,其中控制器是控制台命令,视图是它们的标准输出?
好吧,这一定是一些愚蠢的东西,我做错了,但我找不到它.
MVC行动:
[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult Create(BatchCreateViewModel createModel)
{
return RedirectToRoute(MVC.Home.Display());
}
Run Code Online (Sandbox Code Playgroud)
BatchCreateViewModel:
public class BatchCreateViewModel
{
bool searchAVM;
bool searchBPO;
bool searchAppraisal;
int transactionAge;
string Description;
string uploadfile;
}
Run Code Online (Sandbox Code Playgroud)
View页面上有一些名为"searchAVM","searchBPO","searchAppraisal",(复选框)"transactionAge"(一组带整数值的单选按钮)和"description"(文本框)的控件
当我在"Create"的条目处中断时,createModel就在那里,但是具有所有默认值(对于字符串为null,对于布尔值为false,对于int为0).如果我检查Request.Form,值就在那里,但它们只是没有进入模型.
我究竟做错了什么?
(这是MVC 2,框架4)
我的问题是理想的或原始的MVC解释 http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html
由于MVC的目标是减少依赖性,View应该知道模型吗?然后什么会阻止它变胖,直接调用模型方法而不问控制器?
更新:当我阅读下面的答案时,我将举一个具体的例子:
假设您创建了一个复杂的计算器(不仅仅是一些简单的计算器,让我们说股票市场的一个选项价格).它只需要股票价格,利率,波动率等投入.那么为什么我要创建一个包含视图中方法的整个模型的引用,因为我只需要这些输入变量?
为什么控制器不会在视图中发生变化时通知,然后仅使用输入回调视图中的方法?
例如,我在这里看到View有一个对整个模型的引用:
http://leepoint.net/notes-java/GUI/structure/40mvc.html
private CalcModel m_model;
Run Code Online (Sandbox Code Playgroud) 我有一个视图,内部我正在生成一个列表,但我无法成功将其传递给控制器而没有字段为null:
我的观点代码:
@using (Html.BeginForm("AddFOP", "Revenue", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table class="grid" style="margin-top: 15px">
<thead>
<tr>
<th>FOP</th>
<th>ORGN</th>
<th>PROGRAM</th>
<th>PERCENTAGE</th>
<th></th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.editBillingFOPList.Count;i++ )
{
<tr>
<td>@Html.TextBox("FOPList[" + @i + "].Fund",
Model.editBillingFOPList[i].Fund)</td>
<td>@Html.TextBox("FOPList[" + @i + "].ORGN",
Model.editBillingFOPList[i].ORGN)</td>
<td>@Html.TextBox("FOPList[" + @i + "].Program",
Model.editBillingFOPList[i].Program)</td>
<td>@Html.TextBox("FOPList[" + @i + "].Percentage",
Model.editBillingFOPList[i].Percentage)</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
这是控制器:
[HttpPost]
public ActionResult AddFOP(List<BillingFOPModel> FOPList)
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
我的FOPList显示计数为1,如果我在调试模式下展开它,它会显示模型列表但是具有空值 - 任何想法?
我的型号:
public class …Run Code Online (Sandbox Code Playgroud) 我将PHP对象发送到模板文件,并且希望在模板文件的顶部记录对象的哪些属性(使用__get)可用以及它们是什么,并使它们可用于代码提示.
这是一个例子.
在我的控制器中:
$obj = new Template("welcomePage");
$obj->title = "Welcome!";
$obj->render();
Run Code Online (Sandbox Code Playgroud)
在我的视图/模板中:
<?php
/**
* @var $obj Template The template data wrapper
* @property $obj->title string The page header text
/* ?>
<h1><?php echo $obj->title; ?></h1>
Run Code Online (Sandbox Code Playgroud)
是否有类似的东西可行?如果我开始输入$obj->,我现在拥有它的方式将不会自动完成,这意味着我(或团队成员)需要引用模板的顶部来查找每个可用属性.
我正在考虑为每个模板类型扩展Template类,但这似乎是不必要的开销,因为我可能每页只添加一个字符串和一个数组,并为每个模板创建一个单独的类,部分模板和两者的组合似乎是有点傻.
谢谢〜
我有一个问题,当我从服务器向客户端发送消息时,他们没有按原始顺序到达客户端.我在这里有一个测试功能:
public async Task Hello(string group)
{
await Groups.Add(Context.ConnectionId, group);
await Clients.Group("grp1").Hello("grp1");
await Clients.Group("grp2").Hello("grp2");
await Clients.All.Hello("all");
}
Run Code Online (Sandbox Code Playgroud)
在客户端:
var chanceHub = $.connection.chanceHub;
chanceHub.client.Hello = function (message) {
alert(message);
}
$.connection.hub.start().done(function () {
chanceHub.server.hello("grp1");
});
Run Code Online (Sandbox Code Playgroud)
我希望按此顺序获得2个警报:"grp1"然后"all"但我总是首先得到消息"all".有什么方法可以解决这个问题吗?
免责声明:
正如第一个答案已经适当注意到的那样,在当前的示例案例中使用MVC是过度的.问题的目标是通过一个简单的例子来理解底层概念,以便能够在更大的程序中使用它们来修改更复杂的数据(数组,对象).
我试图在C++和QT中实现MVC模式,类似于这里的问题:
该程序有2行编辑:
3个按钮
并只修改字符串.
与另一个问题的区别在于,我试图实现Subject/Observer模式,以便在模型更改后更新View.
Model.h
#ifndef MODEL_H
#define MODEL_H
#include <QString>
#include <Subject>
class Model : virtual public Subject
{
public:
Model();
~Model();
void convertDecToHex(QString iDec);
void convertHexToDec(QString iHex);
void clear();
QString getDecValue() {return mDecValue;}
QString getHexValue() {return mHexValue;}
private:
QString mDecValue;
QString mHexValue;
};
#endif // MODEL_H
Run Code Online (Sandbox Code Playgroud)
Model.cpp
#include "Model.h"
Model::Model():mDecValue(""),mHexValue(""){}
Model::~Model(){}
void Model::convertDecToHex(QString iDec)
{
mHexValue = iDec + "Hex";
notify("HexValue");
}
void Model::convertHexToDec(QString iHex)
{
mDecValue = iHex …Run Code Online (Sandbox Code Playgroud) 我正在读回DateTime?从我看来的价值.现在我检查一下NextUpdateDateTime?HasValue如果是这样,将时间转换为UTC.
从阅读起来看来,我似乎需要使用a null coalescing operator但是我的任务告诉我System.NUllable不包含ToUniversalTime()使用该运算符时的定义.
我在SO上搜索了类似的问题,但没有运气.
题:
如何将null DateTime值转换为UTC?
码:
我只是检查DateTime?有一个值,如果是这样,将DateTie转换为UTC -
if (escalation.NextUpdate.HasValue)
{
escalation.NextUpdate = escalation.NextUpdate ?? escalation.NextUpdate.ToUniversalTime();
}
else
{
escalation.NextUpdate = null;
}
Run Code Online (Sandbox Code Playgroud)
我NextUpdate在模型中的属性:
public DateTime? NextUpdate { get; set; }
Run Code Online (Sandbox Code Playgroud) c# model-view-controller datetime nullable null-coalescing-operator
这是我的异常类。异常类已经由flutter的抽象异常类实现。我错过了什么吗?
class FetchDataException implements Exception {
final _message;
FetchDataException([this._message]);
String toString() {
if (_message == null) return "Exception";
return "Exception: $_message";
}
}
void loginUser(String email, String password) {
_data
.userLogin(email, password)
.then((user) => _view.onLoginComplete(user))
.catchError((onError) => {
print('error caught');
_view.onLoginError();
});
}
Future < User > userLogin(email, password) async {
Map body = {
'username': email,
'password': password
};
http.Response response = await http.post(apiUrl, body: body);
final responseBody = json.decode(response.body);
final statusCode = response.statusCode;
if (statusCode != HTTP_200_OK || …Run Code Online (Sandbox Code Playgroud)