Witam robię projekt w którym uploaduje obrazek i nie chce mi go dodać do xmla, ale został zapisany tam gdzie zapisuje pliki. Nie wiem już próbuje to rozkminić 2 dzień i nic.
Gdy tworze konto mam mi utworzyć kanał, w account controller w register dodaje obsługę listy do której ma zapisywać username i avatar czyli obrazek ustawiłem na aaa na razie.
Kod :
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
kanal k = new kanal(); //tworze nowy kanal
k.username = model.UserName; //dodaje nazwę użytkownika do pola username
k.avatar = "aaa"; // dodaje jako avatar stringa aaa dla przykładu
// UpdateModel(k);
w.dodajkanal(k); //dodaje kanal
w.Zapisz(); // zapisuje kanal w pliku xml
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
No i tutaj tworzy tego xmla z danymi które chce.
Teraz jak przechodzę do konta ma wyświetlić akcję Details co robi:
Po kliknięciu edit ma otworzyć akcję edit oczywiście no i to też się dzieje.
No i teraz wybieram plik, na razie jest pole username ale to nie ważne później to zmienie i teraz plik wrzuca do folderu gdzie chcę zapisywać pliki, ale nie zmienia się filename avatara w xmlu, w ogóle username też sie nie zmienia.
Co tu moze być nie tak?
Klasa kontolera:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication3.Models;
namespace MvcApplication3.Controllers
{
public class KontoController : Controller
{
obslugakanalu w = obslugakanalu.Wczytaj();
public ActionResult Index()
{
return View();
}
//
// GET: /Konto/Details/5
public ActionResult Details(string username)
{
username = User.Identity.Name;
kanal k = w.pobierzkanal(username);
UpdateModel(k);
w.Zapisz();
return View(w.pobierzkanal(User.Identity.Name));
}
//
// GET: /Konto/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Konto/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Konto/Edit/5
public ActionResult Edit(string username)
{
return View();
}
//
// POST: /Konto/Edit/5
[HttpPost]
public ActionResult Edit(string username,FormCollection collection, HttpPostedFileBase obrazek)
{
try
{
kanal k = new kanal();
string filePath = Path.Combine(Server.MapPath("~/Content/avatary"), Path.GetFileName(obrazek.FileName));
obrazek.SaveAs(filePath);
UpdateModel(k);
k.avatar = Path.GetFileName(obrazek.FileName);
w.dodajkanal(k);
w.Zapisz();
return RedirectToAction("Index", "Plik");
}
catch
{
return View();
}
}
//
// GET: /Konto/Delete/5
public ActionResult Delete(int id)
{
return View();
}
//
// POST: /Konto/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
Widok Edit:
@model MvcApplication3.Models.kanal
@{
ViewBag.Title = "Edit";
}
<h2>Edytuj Avatar</h2>
@using (Html.BeginForm("Edit", "Konto", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>kanal</legend>
<div class="editor-label">
@Html.LabelFor(model => model.avatar)
</div>
<div class="editor-field">
<div class="editor-field">
<input type="file" name="obrazek" />
</div>
</div>
@Html.HiddenFor(model => model.id)
<div class="editor-label">
@Html.LabelFor(model => model.username)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.username)
@Html.ValidationMessageFor(model => model.username)
</div>
<p>
<input type="submit" value="Uploaduj" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>