Microsoft Visual Studio と Microsoft sql server 2008 を使用しています。Web サイトを作成し、ユーザーが SQL テーブルに情報を入力するテキスト値を追加したいと考えています。私はNibarnateとFluentNhibernateを使用しています。しかし、私のコードは価値を追加できません。map 、 entity を作成し、Web ページのテキストから値を追加するにはどうすればよいですか。この問題に対して何ができますか?
テーブル
|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;
}
}
}