model-binding

How can I convert json to a Laravel Eloquent Model?

丶灬走出姿态 提交于 2019-11-28 00:13:32
问题 if I have an Eloquent Model called Post, and the mysql table has: integer ID, string Text How do I convert this JSon: { post: { text: 'my text' } } To the relevant Post object that, once received in the controller, I can save to the database like this: public function store(Post $post) { $post->save(); } I'm not looking to build the logic that would do that for me, but for the Laravel way (or could it be that there isn't one? I googled it with no relevant results). 回答1: Convert json to array

AllowHtml attribute not working

人走茶凉 提交于 2019-11-27 23:20:34
I have a model with this property: [AllowHtml] [DisplayName("Widget for Table")] [StringLength(1000, ErrorMessage = "Maximum chars 1000")] [DataType(DataType.Html)] public object TableWidget { get; set; } And here is the create methods in controller: // // GET: /Admin/Table/Create public ActionResult Create(int id) { Season season = _seasonRepository.GetSeason(id); var table = new Table { SeasonId = season.SeasonId }; return View(table); } // // POST: /Admin/Table/Create [HttpPost] public ActionResult Create(Table a) { if (ModelState.IsValid) { _tableRepository.Add(a); _tableRepository.Save();

Model Binding With Disabled Textbox

二次信任 提交于 2019-11-27 21:00:53
I have a textbox that I am defining as <%= Html.TextBox("Username", Model.Form.Username, new { @class = "textbox", @disabled = "disabled" })%> In my action [AcceptVerbs(HttpVerbs.Post)] [ValidateAntiForgeryToken] public ActionResult EditLogin(LoginForm post) { ... return View(model); } post.Username will be blank, all other properties bind correctly, but if I change @disabled="disabled to @readonly="readonly" the username binds properly and everything works. It looks like model binding ignores values in disabled fields. Is their a way around this? I still need the field's value to bind to the

How do I sub in JSON.NET as model binder for ASP.NET MVC controllers?

旧城冷巷雨未停 提交于 2019-11-27 20:45:26
It has been decided by the ASP.NET Web API team to use the JSON.NET library for model binding JSON data. However, "normal" MVC controllers still use the inferior JsonDataContractSerializer. This causes issues with parsing dates, and is causing me much headache. See this for reference: http://www.devcurry.com/2013/04/json-dates-are-different-in-aspnet-mvc.html The author chooses to solve the issue in the Knockout layer on the client. But I would prefer to solve this by using the same JSON.NET model binder in MVC controllers as in Web API controllers. How do I substitute a different JSON model

ASP.NET MVC model binding an IList<> parameter

情到浓时终转凉″ 提交于 2019-11-27 19:42:40
[I solved this myself, see my answer for cause] I'm having trouble getting form values for a IList<> argument in a controller method set properly. My controller class looks like this: public class ShoppingBasketController : Controller { public ActionResult Index() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Add(IList<ShoppingBasketItem> items) { Session["basket"] = items; // for testing return RedirectToAction("Index"); } } public class ShoppingBasketItem { public int ItemID; public int ItemQuantity; } The slightly trimmed form: <% using (Html.BeginForm("Add",

Passing UTC DateTime to Web API HttpGet Method results in local time

断了今生、忘了曾经 提交于 2019-11-27 18:44:08
I'm trying to pass a UTC date as a query string parameter to a Web API method. The URL looks like /api/order?endDate=2014-04-01T00:00:00Z&zoneId=4 The signature of the method looks like [HttpGet] public object Index(int zoneId, DateTime? endDate = null) The date is coming in as 31/03/2014 8:00:00 PM but I'd like it to come in as 01/04/2014 12:00:00 AM My JsonFormatter.SerializerSettings looks like this new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), DateTimeZoneHandling = DateTimeZoneHandling.Utc, DateFormatHandling = DateFormatHandling

How to bind URL parameters to model properties with different names

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 18:04:10
问题 Okay, lets say I have a URL like so, which is mapped via HTTP verb GET to the controller action I have below: GET /foo/bar?sort=asc&r=true How can I bind this to my model Bar on my controller action I have below: class Bar { string SortOrder { get; set; } bool Random { get; set; } } public ActionResult FooBar(Bar bar) { // Do something with bar return null; } Note that the property names won't and can't necessarily match the names of the URL parameters. Also, these are OPTIONAL url parameters

ASP.NET MVC Model Binder returns null object

和自甴很熟 提交于 2019-11-27 16:31:58
问题 I am having a problem where everytime I post a form back to the [HttpPost] version of my controller action, the ModelBinder returns a null object. I can't work out why. If I change the signature to use a FormCollection instead I can see that all the correct keys have been set. Can someone help me pin point what's wrong here, because I can't spot it. Here are the models for working with my views public class DeviceModel { public int Id { get; set; } [Required] [Display(Name = "Manufacturer")]

Asp.Net MVC 5 bind parameter exclusively from body

天涯浪子 提交于 2019-11-27 16:18:56
问题 I want to prevent posting sensitive data via url query string to a MVC 5 application. In MVC there is a DefaultModelBinder . The DefaultModelBinder looks for the ActionMethod parameters in the url query string, the body and the route. But my target is to bind the parameters exclusively from the body and not from route or query string. In Asp.Net WebApi there is such a concept. The Attribute [FromBody] will do the job: http://www.asp.net/web-api/overview/formats-and-model-binding/parameter

Inject a dependency into a custom model binder and using InRequestScope using Ninject

怎甘沉沦 提交于 2019-11-27 15:37:54
I'm using NInject with NInject.Web.Mvc. To start with, I've created a simple test project in which I want an instance of IPostRepository to be shared between a controller and a custom model binder during the same web request. In my real project, I need this because I'm getting IEntityChangeTracker problems where I effectively have two repositories accessing the same object graph. So to keep my test project simple, I'm just trying to share a dummy repository. The problem I'm having is that it works on the first request and that's it. The relevant code is below. NInjectModule: public class