asp.net-core-mvc

ASP.NET Core - Changing form values after a post

自古美人都是妖i 提交于 2019-12-01 17:33:23
I'm probably missing something very obvious, but all I'm trying to do is have a simple form, allow it to POST, change one of the fields, and when it returns it should show me the changed value. Simple, right? The model looks like this: public class TestModel { public string Field1 { get; set; } public string Field2 { get; set; } } The controller actions: public IActionResult Test() { return View(new TestModel()); } [HttpPost] public IActionResult Test(TestModel model) { model.Field1 = "changed"; // this seems to be ignored return View(model); } The view: @model AspNetCoreTest1.Models.TestModel

How can I call a controller action when rendering a partial view?

大憨熊 提交于 2019-12-01 17:31:18
问题 I am creating a partial view for a sidebar that will show the most popular posts in my site. How can I create a separated controller for loading the model required by the partial view? (The IEnumerable<Post> with the popular posts) Currently I have created a controller class that loads the popular posts but I keep getting errors when rendering the partial as I cannot call the controller and load the partial model. For example, if I call it from a view where I render a single post, the model

Asp.Net Core Web API 2.2 Controller not returning complete JSON

爷,独闯天下 提交于 2019-12-01 17:30:12
I have a Web API Controller in my Asp.Net Core Web API 2.2 project. Messageboard model: public class MessageBoard { public long Id { get; set; } public string Name { get; set; } public string Description { get; set; } public ICollection<Message> Messages { get; set; } } Message model: public class Message { public long Id { get; set; } public string Text { get; set; } public string User { get; set; } public DateTime PostedDate { get; set; } public long MessageBoardId { get; set; } [ForeignKey("MessageBoardId")] public MessageBoard MessageBoard { get; set; } } This is one of my Web API

Nuget cannot find newer dependency

拜拜、爱过 提交于 2019-12-01 17:06:06
I've just created a new project in ASP 5 MVC 6 beta8 and a compatible class library for tests. The problem occurs in this new "Web Class Library" project that I intended to use for tests. This is what my project.json looks like: { "version": "1.0.0-*", "description": "ClassLibrary1 Class Library", "authors": [ "Me" ], "tags": [ "" ], "projectUrl": "", "licenseUrl": "", "frameworks": { "dnx451": { } }, "dependencies": { "AutoFixture": "3.36.9", "AutoFixture.AutoMoq": "3.36.9", "Moq": "4.2.1510.2205" } } During compilation I get the following error: Severity Code Description Project File Line

Getting hold of raw POST data when using [FromBody]

谁说胖子不能爱 提交于 2019-12-01 16:45:19
I have a controller running on ASP.NET Core 1.0 RC2 and I'd like to dump the raw POST data out to telemetry as ApplicationInsights doesn't do this for you. My code looks like this [HttpPost] [Produces("application/json")] public IActionResult Post([FromBody] RequestClass RequestData) { var stream = this.HttpContext.Request.Body; stream.Position = 0; using (var reader = new StreamReader(stream)) { string body = reader.ReadToEnd(); Telemetry.TrackTrace(body, Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information); } return Ok(); } But the string "body" always comes up empty. If I

Nuget cannot find newer dependency

别说谁变了你拦得住时间么 提交于 2019-12-01 16:34:20
问题 I've just created a new project in ASP 5 MVC 6 beta8 and a compatible class library for tests. The problem occurs in this new "Web Class Library" project that I intended to use for tests. This is what my project.json looks like: { "version": "1.0.0-*", "description": "ClassLibrary1 Class Library", "authors": [ "Me" ], "tags": [ "" ], "projectUrl": "", "licenseUrl": "", "frameworks": { "dnx451": { } }, "dependencies": { "AutoFixture": "3.36.9", "AutoFixture.AutoMoq": "3.36.9", "Moq": "4.2.1510

Asp.Net Core Web API 2.2 Controller not returning complete JSON

牧云@^-^@ 提交于 2019-12-01 16:12:47
问题 I have a Web API Controller in my Asp.Net Core Web API 2.2 project. Messageboard model: public class MessageBoard { public long Id { get; set; } public string Name { get; set; } public string Description { get; set; } public ICollection<Message> Messages { get; set; } } Message model: public class Message { public long Id { get; set; } public string Text { get; set; } public string User { get; set; } public DateTime PostedDate { get; set; } public long MessageBoardId { get; set; } [ForeignKey

Required query string parameter in ASP.NET Core

耗尽温柔 提交于 2019-12-01 16:12:19
Using ASP.NET Core 1.1 with VS2015 (sdk 1.0.0-preview2-003131), I have the following controller: public class QueryParameters { public int A { get; set; } public int B { get; set; } } [Route("api/[controller]")] public class ValuesController : Controller { // GET api/values [HttpGet] public IEnumerable<string> Get([FromQuery]QueryParameters parameters) { return new [] { parameters.A.ToString(), parameters.B.ToString() }; } } As you can see, I have two query parameters. What I would like is to have one of them (ex: A ) to be required. That is, I would like to use an attribute (if possible) to

Why BindNever attribute doesn't work

旧城冷巷雨未停 提交于 2019-12-01 16:00:46
问题 I do not want do bind the Id property on my CustomerViewModel so I added a [BindNever] attribute but it is not working. What could be the solution? I have the following: CustomerController.cs // PUT api/customers/5 [HttpPut("{id}")] public async Task<IActionResult> Put([FromUri] int id, [FromBody]CustomerViewModel customer) { //Implementation } CustomerViewModel public class CustomerViewModel { [BindNever] public int Id { get; set; } public string LastName { get; set; } public string

Typeconverter does not work in asp.net core

妖精的绣舞 提交于 2019-12-01 15:53:44
I have Amount stored in the database as decimal . I want to show that value on UI with thousand separator. I can add [DisplayFormat(DataFormatString = "{0:N2}", ApplyFormatInEditMode = true)] attribute on amount property and that would display number with thousand separator however when i POST the value back to server, the MVC model binding would not work because of commas. I have created a custom type converter that converts from decimal to string and then string to decimal public class NumberConverter : TypeConverter { public override bool CanConvertFrom( ITypeDescriptorContext context, Type