我正在尝试通过转换我拥有的Web表单应用程序来学习asp.net MVC.这是一个房间预订应用程序,其中有一个客户表(tblCustomerBooking)与tblRental有一对多的关系 - 所以一个客户可以预订多个房间.彼此匹配的字段是tblCustomerBooking.customer_id - > tblRental.customer_ref
我正在尝试首先使用代码 - 并构建一个模型类 - 但我无法弄清楚如何链接这两个表,因此当我查询dbContext时,它将返回一个客户,其中有一个或多个租用同一型号.
我的表定义是:
CREATE TABLE [dbo].[tblCustomerBooking](
[customer_id] [bigint] IDENTITY(1,1) NOT NULL,
[room_id] [bigint] NULL,
[customer_name] [varchar](110) NULL,
[customer_email] [varchar](50) NULL
CONSTRAINT [PK_tblCustomerBooking] PRIMARY KEY CLUSTERED
(
[customer_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
CREATE TABLE [dbo].[tblRental](
[rental_id] [bigint] IDENTITY(1,1) NOT NULL,
[room_id] [bigint] NOT NULL,
[check_in] [datetime] NOT NULL,
[check_out] [datetime] NOT …
Run Code Online (Sandbox Code Playgroud) 这是保持对数据库行的更改的简单跟踪的最佳方法:
ALTER TRIGGER [dbo].[trg_121s]
ON [dbo].[121s]
AFTER UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
update dbo.[121s]
set modified=getdate()
where id in
(select distinct ID from Inserted)
END
Run Code Online (Sandbox Code Playgroud)
因此,对[121s]中某行的任何更新都将导致修改的列被更新.
它有效,但我不确定它是否是实现这一目标的最佳方式.
我对这条线路感到困惑:
(select distinct ID from Inserted)
Run Code Online (Sandbox Code Playgroud)
...以及它如何知道它获得正确的行ID.
感谢您的任何确认/澄清,
标记
我需要生成QR图像 - 但URL需要包含参数.
我试过Googles:
http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=http://www.me.com/me.asp?i=1&n=2
Run Code Online (Sandbox Code Playgroud)
...但生成的图像只链接到:
http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=http://www.me.com/me.asp?i=1
Run Code Online (Sandbox Code Playgroud)
也就是说,最后没有n = 2.
有谁知道如何生成QR图像,这将允许多个参数?
在我的模型中,我有:
[Required]
[DataType(DataType.EmailAddress)]
public string EmailAddress {get; set;}
Run Code Online (Sandbox Code Playgroud)
但是在我看来(来源)这会呈现为:
<label for="EmailAddress">EmailAddress</label>
<input data-val="true" data-val-required="The EmailAddress field is required"
id="EmailAddress" name="EmailAddress" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="EmailAddress" data-valmsg-replace="true">
Run Code Online (Sandbox Code Playgroud)
我认为应该使用type ="email"而不是type ="text" 进行渲染- 因此在客户端进行验证时,如果电子邮件的格式不是有效,则不会提取.
编辑
此外,当我调用if(ModelState.IsValid)时,它返回true,即使电子邮件地址的格式不正确.所以即使浏览器不支持HTML5,我也会认为控制器中的IsValid会强制执行验证 - 不是这样吗?
在我的模型上是否还需要其他东西,强制输入type ="email"以便正确检查有效的电子邮件地址?
谢谢,
标记
我从http://dev.mysql.com/downloads/connector/net/安装了MySql Connector 6.6.5
我在asp.net MVC网站上添加了它作为参考(请参见下面的截图),并将"Copy local"更改为true.
但是,当我到达该行时:var calls = db.Calls.ToList();
我收到错误:
Unable to find the requested .Net Framework Data Provider. It may not be installed.
我在下面提供了我的代码.任何人都可以让我知道我错过了什么?
谢谢,马克
Controller Call.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MySql.Data;
using MySql.Data.Entity;
using MySql.Data.MySqlClient;
using MySql.Web;
using System.Data;
namespace bm.Controllers
{
public class CallController : Controller
{
private CallContext db = new CallContext();
//
// GET: /Calls/
public ActionResult Index()
{
var calls = db.Calls.ToList();
return …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用ViewModels和AutoMapper,因为我知道这些是避免许多问题的最佳实践.
我可以使用AutoMapper来填充视图模型,但是我不确定如何更新我的数据库,从ViewModel发回到我的控制器.
我的GET是:
public ActionResult Edit(int id = 0)
{
Customer customer = db.Customers.Find(id);
var offers = db.Offers.Where(x => x.CustomerId == id).ToList();
var email = db.Emails.FirstOrDefault();
var vm = new CreateViewModel();
vm.CustomerId = customer.CustomerId;
vm.ArrivalDate = customer.ArrivalDate;
vm.CustomerName = customer.CustomerName;
vm.EmailAddress = customer.EmailAddress;
vm.NumNights = customer.NumNights;
vm.NumPeople = customer.NumPeople;
vm.EmailBody = email.EmailBody;
vm.From = email.From;
vm.Subject = email.Subject;
// Map list of Offers into ViewModel
vm.Offers = Mapper.Map<IList<Offer>, IList<OfferVM>>(offers);
return View(vm);
}
Run Code Online (Sandbox Code Playgroud)
我的帖子是:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(EditViewModel vme)
{ …
Run Code Online (Sandbox Code Playgroud) 我在以下方面收到错误System.ArgumentNullException: Value cannot be null.
:
foreach (var pmt in payment.NewInvoiceViewModels
.Where(x => x.PaymentReceived != 0) ??
Enumerable.Empty<NewInvoiceViewModel>())
Run Code Online (Sandbox Code Playgroud)
我要做的就是检查payment.NewInvoiceViewModels在我迭代之前为空(如果它为null,则会导致错误).
有没有更好的方法来实现这一目标?
我正在使用编辑器模板 - 因为我需要遍历我的模型,并在我的模型中显示/编辑许多RatesList对象.
这可确保ID和名称正确 - 因此它们会将Post上的模型绑定回控制器.
但是下拉列表需要是动态的 - 所以必须从0显示我的Model.TypeCount.
无论如何我使用@ Html.DropDownListFor帮助器 - 以便正确命名下拉列表?在我的下面的代码中,它只是一个select语句 - 因为我不知道如何使下拉列表动态 - 但是然后没有使用命名约定,并且下拉列表没有绑定回我的模型.我的编辑器视图是:
@model ebs.Models.RatesList
@{ Layout = null; }
<tr>
<td>
@Html.TextBoxFor(modelItem => modelItem.TypeID)
</td>
<td>
@{ var num = Model.TypeCount; }
<select id="NumSelected" name="NumSelected">
@for (var i = 0; i <= num; i++)
{
<option value="@i">@i</option>
}
</select>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助,
标记
是否可以合并 DBContext 和 IdentityDbContext - 启用迁移,然后使用一个更新语句管理对我的数据库的代码优先更新?
IE。在我的IdentityModels.cs
文件中,我有:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("tbConn", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的tbContext.cs
文件中(当我添加模型类时创建的)我有:
namespace tb.Models
{
public class tbContext : DbContext
{
public tbContext() : base("name=tbConn")
{
Database.SetInitializer<tbContext>(new tbInitializer<tbContext>());
}
public System.Data.Entity.DbSet<tb.Models.Tour> Tours { get; set; }
}
}
public class tbInitializer<T> : DropCreateDatabaseAlways<tbContext>
{
protected override void Seed(tbContext context)
{
ApplicationDbContext userscontext = new ApplicationDbContext();
var …
Run Code Online (Sandbox Code Playgroud) 我继承了一个数据库,我需要查询它.
但是,当我在下面运行我的代码时,我收到错误:
{"The 'solution' property on 'QA' could not be set to a 'System.Byte' value. You must set this property to a non-null value of type 'System.Int32'. "}
表定义是:
CREATE TABLE [dbo].[qa](
[id] [int] IDENTITY(1,1) NOT NULL,
[r3_3] [tinyint] NULL,
[r6_3] [tinyint] NULL,
[r6_13] [tinyint] NULL,
[datesaved] [datetime] NULL
CONSTRAINT [PK_qa] PRIMARY KEY CLUSTERED
(
[id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Run Code Online (Sandbox Code Playgroud)
所以r3_3,r_3,r6_13可以包含1,0或null. …
asp.net-mvc ×6
asp.net ×4
c# ×3
html ×2
code-first ×1
linq ×1
mysql ×1
qr-code ×1
razor ×1
sql ×1
sql-server ×1
url ×1
validation ×1