我使用 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>
动作.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;
}
}
}