Index.cshtml code for text box
@Html.LabelFor(m => m.ValidFromDate, "ValidFromDate")
@Html.TextBoxFor(m => m.ValidFromDate)
我不知道如何将此文本框值插入数据库
答案 0 :(得分:0)
假设您在数据库中有一个表名用户。首先,我们将创建一个POCO
,它将代表此表,如下所示:
public class User
{
public string Username { get; set; }
public DateTime ValidFromDate { get; set; }
//more propteries
}
现在让我们假设我们有一个名为Create的视图。在此视图中,您将通过我们已创建的User
类作为您的模型。所以你的观点看起来应该是这样的。
@model Models.User
@{
ViewBag.Title = "User";
}
<div>
@using (Html.BeginForm("Create", "User"))
{
@Html.AntiForgeryToken()
@Html.LabelFor(m => m.Username)
@Html.TextBoxFor(m => m.Username)
@Html.LabelFor(m => m.ValidFromDate)
@Html.TextBoxFor(m => m.ValidFromDate)
<button type="submit">Submit</button>
}
</div>
现在,您希望在控制器操作中获取此视图中的所有值。我们将继续创建名为UserController
的控制器,在此控制器中,我们将GET
和POST
方法提供Create
//This is our GET action for the view
public ActionResult Create()
{
User user = new User();
return View(user); //we pass in the user object to the view
}
[HttpPost]
[ValidateAntiForgeryToken]
//when you click submit this is the action that will get hit
public ActionResult Create(User model)
{
if (!ModelState.IsValid)
return View(model);
string userName = model.Username;
DateTime validFrom = model.ValidFromDate;
//save to db here. I'm not sure if you're making direct calls or using entity framework or similar framework
return RedirectToAction("Index", "Home"); //this will redirect the user to home. You can return a view or redirect user somewhere else.
}
答案 1 :(得分:0)
我们走了。我测试了它,它返回了模型属性以及发布的文件。
DECLARE @Tables TABLE
(
myTableName VARCHAR(500)
)
insert into @Tables(myTableName) values ('WorkOrderMasterAttachments');
insert into @Tables(myTableName) values ('BillOfMaterialAttachments');
insert into @Tables(myTableName) values ('CustomerAttachments');
insert into @Tables(myTableName) values ('ToolAttachments');
insert into @Tables(myTableName) values ('FacilityAttachments');
insert into @Tables(myTableName) values ('LocationAttachments');
insert into @Tables(myTableName) values ('AssetAttachments');
insert into @Tables(myTableName) values ('ContractorAttachments');
insert into @Tables(myTableName) values ('VendorAttachments');
insert into @Tables(myTableName) values ('WorkOrderAttachments');
insert into @Tables(myTableName) values ('ClosedWorkOrderAttachments');
insert into @Tables(myTableName) values ('EmployeeAttachments');
insert into @Tables(myTableName) values ('PurchaseOrderAttachments');
insert into @Tables(myTableName) values ('PurchaseOrderHistoryAttachments');
insert into @Tables(myTableName) values ('PartAttachments');
insert into @Tables(myTableName) values ('AssetSystemAttachments');
declare @SQL nvarchar(max) = ''
select @SQL = @SQL + 'Update ' + myTableName + ' set Path = REPLACE(Path,@oldPath,@newPath);'
from @Tables
select @SQL
exec sp_executesql @SQL, N'@oldPath varchar(500), @newPath varchar(500)', @oldPath = @oldPath, @newPath = @newPath
- 这是我用来绑定表单的模型
//-- this is the controller
public class FileUploadDemoController : Controller
{
//
// GET: /FileUploadDemo/
public ActionResult Index()
{
return View();
}
-- here you can name the method as you like to I justuse postform
[HttpPost]
public ActionResult PostForm(FileUploadModel model)
{
Request.Files is the object which contrins all files which you posted from the form when you hit submit buuton after selecting excel file.
if (Request.Files != null && Request.Files.Count > 0)
{
// here you have files attached and model propertied now you can go to db and perform operation which you need to do
}
return View("Index", model);
}
}
- 最后是cshml文件。导入点是在BeginForm中使用新的{enctype =“multipart / form-data”}。
namespace WebApplication1.Models
{
public class FileUploadModel
{
public string Name { get; set; }
public string ValidFromDate { get; set; }
}
}
//如果您输入01-01-2015这样的日期进行测试,请不要使用任何日期票据来保持简单。
// the page from where you will post the data . Please change you model class in place of FileUploadModel I created ot for me .