我使用Microsoft Visual Studio和Microsoft sql server 2008.我创建了网站,我想添加文本值,用户在sql表上输入信息。我使用Nhibarnate和FluentNhibernate。但我的代码无法增加价值。我创建地图,实体以及如何从网页文本中添加值。我能为这个问题做些什么?
表格
|Address table|
--------------
|AddressId | int
------------------------
|FullName | varchar
------------------------
|AddressLine1 | varchar
------------------------
|AddressLine2 | varchar
------------------------
|city | varchar
------------------------
|State | varchar
------------------------
|zip | varchar
------------------------
|country | varchar
------------------------
FluentlyConfig.cs
namespace Deneme.Config
{
public class FluentlyConfig
{
private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private ISessionFactory SessionFactory { get; set; }
private FluentlyConfig()
{
SessionFactory = CreateSessionFactory();
}
public static FluentlyConfig Instance
{
get
{
return FluentNhibernateConfigFactory.instance;
}
}
public class FluentNhibernateConfigFactory
{
static FluentNhibernateConfigFactory() { }
internal static readonly FluentlyConfig instance = new FluentlyConfig();
}
public ISessionFactory CreateSessionFactory()
{
try
{
if (this.SessionFactory == null)
{
return Fluently.Configure()
.Database(
MsSqlConfiguration
.MsSql2008
.ConnectionString(c => c
.FromConnectionStringWithKey("DB"))
.DefaultSchema("dbo")
)
.Mappings(m =>
{
m.FluentMappings
.AddFromAssemblyOf<Address>()
.Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Never());
})
.BuildSessionFactory();
}
else { return SessionFactory; }
}
catch (Exception ex)
{
logger.Fatal("Bağlantı Oluşturulamadı", ex);
return null;
}
}
}
}
控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Deneme.Controllers
{
public class Default1Controller : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
return View();
}
public JsonResult deneme( String AddressId, String FullName, String AddressLine1 ,String AddressLine2 ,String City ,String State, String Zip ,String Country )
{
return new JsonResult();
}
}
}
实体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Deneme.Entities
{
public class Address
{
public virtual int AddressId {get; set;}
public virtual string FullName {get; set;}
public virtual string AddressLine1 {get; set;}
public virtual string AddressLine2 {get; set;}
public virtual string City {get; set;}
public virtual string State {get; set;}
public virtual string Zip {get; set;}
public virtual string Country {get; set;}
//AddressId FullName AddressLine1 AddressLine2 City State Zip Country
}
}
映射
using Deneme.Entities;
using FluentNHibernate.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Deneme.Mapping
{
public class AddressMap : ClassMap<Address>
{
public AddressMap()
{
Table("Address");
Id(x => x.AddressId).Column("Id").GeneratedBy.Identity();
Map(x => x.FullName).Column("Fullname");
Map(x => x.AddressLine1).Column("AddressLine1");
Map(x => x.AddressLine2).Column("AddressLine2");
Map(x => x.City).Column("City");
Map(x => x.State).Column("State");
Map(x => x.Zip).Column("Zip");
Map(x => x.Country).Column("Country");
}
}
}
Index.cshtml文本部分和js部分。我想点击按钮后发送值
按钮
<p><a href="button1" class="btn btn-primary btn-large">Send »</a></p>
地址表单部分
<!-- full-name input-->
<div class="control-group">
<label class="control-label">Full Name</label>
<div class="controls">
<input id="full-name" name="full-name" type="text" placeholder="full name"
class="input-xlarge">
<p class="help-block"></p>
</div>
</div>
<!-- address-line1 input-->
<div class="control-group">
<label class="control-label">Address Line 1</label>
<div class="controls">
<input id="address-line1" name="address-line1" type="text" placeholder="address line 1"
class="input-xlarge">
<p class="help-block">Street address, P.O. box, company name, c/o</p>
</div>
</div>
<!-- address-line2 input-->
<div class="control-group">
<label class="control-label">Address Line 2</label>
<div class="controls">
<input id="address-line2" name="address-line2" type="text" placeholder="address line 2"
class="input-xlarge">
<p class="help-block">Apartment, suite , unit, building, floor, etc.</p>
</div>
</div>
<!-- city input-->
<div class="control-group">
<label class="control-label">City / Town</label>
<div class="controls">
<input id="city" name="city" type="text" placeholder="city" class="input-xlarge">
<p class="help-block"></p>
</div>
</div>
<!-- region input-->
<div class="control-group">
<label class="control-label">State / Province / Region</label>
<div class="controls">
<input id="region" name="region" type="text" placeholder="state / province / region"
class="input-xlarge">
<p class="help-block"></p>
</div>
</div>
<!-- postal-code input-->
<div class="control-group">
<label class="control-label">Zip / Postal Code</label>
<div class="controls">
<input id="postal-code" name="postal-code" type="text" placeholder="zip or postal code"
class="input-xlarge">
<p class="help-block"></p>
</div>
</div>
<!-- country select -->
<div class="control-group">
<label class="control-label">Country</label>
<div class="controls">
<select id="country" name="country" class="input-xlarge">
<option value="" selected="selected">(please select a country)</option>
<option value="AF">Afghanistan</option>
<option value="AL">Albania</option>
<option value="DZ">Algeria</option>
<option value="AS">American Samoa</option>
<option value="AD">Andorra</option>
<option value="AO">Angola</option>
<option value="AI">Anguilla</option>
<option value="AQ">Antarctica</option>
<option value="AG">Antigua and Barbuda</option>
<option value="AR">Argentina</option>
</select>
</div>
</div>
java脚本部分
@section jscript
{
<script type="text/javascript">
$("#button1").live("click", function () {
$.ajax({
type: "POST",
url: path + "Default1/deneme",
contentType: "application/json;charset=utf-8",
data: "{FullName:\"" + $("#full-name").val() +
"}",
data: "{AddressLine1:\"" + $("#address-line1").val() +
"}",
data: "{AddressLine2:\"" + $("#address-line2").val() +
"}",
data: "{ City:\"" + $("#city").val() +
"}",
data: "{State:\"" + $("#region").val() +
"}",
data: "{Zip:\"" + $("#postal-code").val() +
"}",
data: "{Country:\"" + $("#country").val() +
"}",
success: function (data) {
},
error: function (xhr, ajaxOptions, thrownError) {
$("#button1").removeAttr("disabled");
alert(thrownError);
}
});
});
</script>
}
和最后一个Web.config。我在这个部分做数据库和w.studio连接。
<connectionStrings>
<add name="DB" connectionString="server=A88;uid=ABC;pwd=123;database=DS"/>
</connectionStrings>
Action.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Deneme.Entities;
namespace Deneme
{
public class Actions
{
public bool SaveData(string AddressId, string FullName, string AddressLine1, string AddressLine2, string City, string State, string Zip, string Country)
{
Address adress = new Address();
adress.City= City;
adress.AddressLine1 = AddressLine1;
adress.AddressLine2 = AddressLine2;
adress.Country = Country;
adress.FullName = FullName;
adress.State = State;
adress.Zip = Zip;
return true;
}
}
}
答案 0 :(得分:0)
简单形式的答案是打开一个会话,开始一个事务然后提交。有点像: -
public bool SaveData(...)
{
using (var session = Deneme.Config.FluentlyConfigure.SessionFactory.OpenSession())
{
using (var tran = session.BeginTransaction())
{
Address adress = new Address();
adress.City= City;
...
session.Save(address);
tran.commit();
}
}
return true;
}
看起来您可能需要重构FluentlyConfig
类来展示SessionFactory
。您可能希望查看sesion-per-request以帮助您在NHibernate中进行会话管理。
另外,我会检查你的AJAX POST,因为它看起来也是正确的。
答案 1 :(得分:0)
直接我可以看到你的AJAX调用是完全错误的。您只传入一个data
对象。
$.ajax({
type: "POST",
url: path + "Default1/deneme",
contentType: "application/json;charset=utf-8",
data: {
FullName: "",
AddressLine1: "",
AddressLine2: "",
City: "",
State: "",
Zip: "",
Country: ""
},
success: function (data) {
},
error: function (xhr, ajaxOptions, thrownError) {
$("#button1").removeAttr("disabled");
alert(thrownError);
}
});
然后,您需要将JsonResult
deneme
与其余代码相关联。